Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for geometry fields in SQL mappers #361

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Lawrence72
Copy link

Currently its not possible to insert/update geometry fields with a SQL mapper.

This adds support for this

How to use:

$this->User_Mapper->position =  'POINT (30 10)';
$this->User_Mapper->save();

This also works with other geometry types:

$this->User_Mapper->position = 'LINESTRING(2 1, 6 6)';

$this->User_Mapper->position = 'POLYGON((0 5, 2 5, 2 7, 0 7, 0 5))';

Retrieving geometry information can be done using a virtual field

$this->User_Mapper->distance = "ST_Distance(POINT(2,1), position)";
$this->User_Mapper->load(['id = ?', 1]);
echo $this->User_Mapper->distance;

@ikkez
Copy link
Member

ikkez commented Apr 30, 2023

@Lawrence72 How is the field type geometry detected, or did you set that manually somewhere?

@Lawrence72
Copy link
Author

@ikkez In the mapper class, as part of the construct it grabs the table schema, and part of that is the field types (F3 Mapper already does this), this is all stored in the $this->fields array. So I am checking the info stored in there to see if the field is set to type geometry and behaving a little differently if it is. Its not a user set variable or anything the user can alter in their mapper call.

@ikkez
Copy link
Member

ikkez commented Jul 23, 2023

Hi again.. sorry for the delay. Does this work for all sql engines or have you only tried MySQL/MariaDB ?
I'll definitely have a look at this again.

@Lawrence72
Copy link
Author

Ive only tested it on MYSQL/Maria.

Here is a gist of the test I ran to work out what the issue was:
https://gist.github.com/Lawrence72/0fada42e673abd41600190b4035dfad3

The SQL queries work fine, the select from a mapper works as long as its added as a virtual field

The insert does not work. What I found was that the mapper didnt handle SQL functions in the mapper field value which is required in order to add a geo spacial. ( ST_GeomFromText('POINT(4 4)') )

So the solution I was trying to do was to take out the need for the ST_GeomFromText by detecting that the field was of type geometry and wrap it in a ST_GeomFromText($value) so the mapper could be set to $mapper->field = 'POINT(4 4)' and have it insert correctly.

The alternative that I can think of for this would be a more general fix where the mapper allows SQL functions in the mapper field value.

Though when I was looking at it I thought that might cause problems.

Another idea I had was to allow something similar to virtual fields in an insert. That would make it so SQL functions could be added but under more controlled conditions than trying to detect when the value includes a SQL function and when it doesn't.

I hope this makes sense, on what I was trying to do with it.

@KOTRET
Copy link
Contributor

KOTRET commented Aug 9, 2023

the function to call depends on the dbms. For SQL Server it's like geometry::STGeomFromText('<geometry_tagged_text>', <SRID >), so this maybe should not be hardcoded at this point

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants