Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring native Point PostgreSQL Type in Doctrine2

Trying to do the same as here, with Postgres:

http://codeutopia.net/blog/2011/02/19/using-spatial-data-in-doctrine-2/

I have a problem with the "convertToDatabaseValue" function (see here : http://doctrine-dbal.readthedocs.org/en/latest/reference/types.html#custom-mapping-types)

My Custom Type :

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

class PointType extends Type
{
const POINT = 'point';

public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
    return 'POINT';
}

public function convertToPHPValue($value, AbstractPlatform $platform)
{
    return new Point($value[0],$value[1]);
}

public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
    return "'".$value->getLongitude().','.$value->getLatitude()."'";
}

public function getName()
{
    return self::POINT;
}
}

Manualy, I can insert this Point with following SQL :

INSERT INTO points (pointtype,name,score) VALUES ('2.43145,42.84214',"My Place",3241)

But doctrine DBAL, creates following code :

'INSERT INTO points (pointtype,name,score) VALUES (?,?,?)' with params ["'2.43145,42.84214'","My Place",3241]

-> My Point is interpreted as a string ...

Can I avoid that ?

CAUTION: I don't want to use PostGIS, juste native Points from PostgreSQL

like image 267
Alsatian Avatar asked Oct 22 '25 14:10

Alsatian


1 Answers

It is because you need to return just string and Doctrine will automatically wrap it with quotes. Try this code:

public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
    return $value->getLongitude().','.$value->getLatitude();
}

It will generate you next SQL query:

INSERT INTO points (pointtype,name,score) VALUES ("2.43145,42.84214","My Place",3241)
like image 166
Michael Sivolobov Avatar answered Oct 25 '25 12:10

Michael Sivolobov