How can I extend Doctrine 2 to handle MySQL spatial ("point" field type) in CodeIgniter 2? How can I extend Doctrine 2 to handle MySQL spatial ("point" field type) in CodeIgniter 2? codeigniter codeigniter

How can I extend Doctrine 2 to handle MySQL spatial ("point" field type) in CodeIgniter 2?


It was a long time ago so I've forgotten some details, but I put these 4 files in a folder called CodeIgniter/application/libraries/Doctrine/CustomAddon:

  • Distance.php
  • Point.php
  • PointStr.php
  • PointType.php

I set the namespace of each as "CustomAddon".

Then at the bottom of CodeIgniter/application/libraries/Doctrine.php I put:

require_once APPPATH . 'libraries/Doctrine/CustomAddon/Distance.php';require_once APPPATH . 'libraries/Doctrine/CustomAddon/Point.php';require_once APPPATH . 'libraries/Doctrine/CustomAddon/PointStr.php';require_once APPPATH . 'libraries/Doctrine/CustomAddon/PointType.php';Doctrine\DBAL\Types\Type::addType('point', 'CustomAddon\PointType');//Assuming $config is a Doctrine\ORM\Configuration object used to configure the EM$config->addCustomNumericFunction('DISTANCE', 'CustomAddon\Distance');$config->addCustomNumericFunction('POINT_STR', 'CustomAddon\PointStr');// Create EntityManager$this->em = EntityManager::create($connectionOptions, $config);

Then usage looks something like this:

$point = new \CustomAddon\Point($geo['lat'], $geo['lon']);


I work with doctrine 1.2 so I don't know if this will work for you. With 1.2.4 you can declare a point type in your yaml or in your model and it translates fine to the database. To save data as a Point you need to create a new doctrine expression like this:

$phone->point = new Doctrine_Expression("GEOMFROMTEXT('POINT(25 10)')");

With the data you need instead those 25 and 10, of course. With doctrine 1.2.4 this will work fine and save the data in the DB. To retrieve your data you just have to use this kind of select statement:

$query = Doctrine_Query::create()         ->select ('X(p.point), Y(p.point)')         ->from('Points p');

And you will have an array called point with two elements (x and y) in your object. As i said, I work with 1.2.4 but if there is an easy way to work with points in that version something similar has to work with doctrine 2. Good luck!