Question

I'm using PropelORM 1.6 and implementing some kind of GIS service for a project. In database (MySQL), for coordinates, I'm using field type of POINT to store the coordinates for diverse items.

In schema.xml for building table model, I've set this (POINT) field to VARCHAR(255), because AFAIK spatial data types are not yet supported.

Organizing select queries for this field is Ok using Criteria::CUSTOM, but when I want to update this field, using well known GeomFromText with Propel, I'm getting next error:

Warning: PDOStatement::execute(): SQLSTATE[22003]: Numeric value out of range: 1416 Cannot get geometry object from data you send to the GEOMETRY field in /var/www/.../propel/util/BasePeer.php on line 425

I'm setting field value using

$object->setGeo("GeomFromText( 'POINT(48.211055 16.383728)' )");

I suppose that this string is treated like a string value by ORM and GeomFromText is not treated as a function like it should be.

Unfortunately there is no Criteria::CUSTOM for setting field value.

How can I update such fields with PropelORM ?

UPDATE: In ZendFramework there is Zend_Db_Expr for this kind of tasks, maybe there is something similar in PropelORM ?

Was it helpful?

Solution

The solution I've found for now is to use custom query and to update GEOMETRY field after record is saved.

Here is the function I'm using to extend functionality.

/**
 * Update spatial data field from numerical values of record
 * If record ID is not defined - all records will be updated from their own values
 *
 * @param int $record Record ID to update
 */
public function updateGEO($record = null) {

    $sql = 'UPDATE `'.TablePeer::TABLE_NAME.'` SET '.TablePeer::GEO.' = GeomFromText(CONCAT( \'POINT(\', '.TablePeer::LATITUDE.', \' \', '.TablePeer::LONGITUDE.', \')\' ))'.(!empty($record) ? ' WHERE '.TablePeer::ID.' = '.$record : null);

    return \Propel::getConnection(TablePeer::DATABASE_NAME)
            ->prepare($sql)
            ->execute();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top