Very quick post today, but I’m recently migrating a MySQL database to a Postgres one because the guys using it want to now take advantage of the geographical possibilities of Postgres. Some tables were very easy to migrate with very basic scripts however for some of them with lots of relations to each other, I wanted to take advantage of using an ORM to make things more simple for myself.
But Doctrine2 doesn’t have native capabilities for PostGIS. It does however offer the possibility to define your own database types. So here’s what I did: define a geotype like this:
I return null for the sql declaration and the PHP values as I don’t need these. I will not ask doctrine to create these fields, will not even read from these fields. All I’m doing is write them. The syntax is `POINT(longitude latitude)`. The SRID is just a standard parameter set to 4326 for geometry queries in Postgres. So when I receive a value (my value is set as a string `$longitude $latitude`) I just set it in the appropriate format for Postgres to understand.
In my class I just need to declare a column of the ‘geotype’:
And that’s it! You’re done (well almost, you also need to register your type and add these two lines somewhere in your code:
That was really easy, simple and straightforward for me.