在Doctrine2中配置本机Point PostgreSQL类型

时间:2014-12-08 12:11:32

标签: postgresql symfony doctrine-orm doctrine

尝试使用Postgres执行相同的操作:

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

我的“convertToDatabaseValue”函数有问题 (见这里:http://doctrine-dbal.readthedocs.org/en/latest/reference/types.html#custom-mapping-types

我的自定义类型:     

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,我可以使用以下SQL插入此Point:

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

但是教条DBAL创建了以下代码:

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

- >我的点被解释为一个字符串......

我可以避免吗?

注意:我不想使用PostGIS中的PostGIS,juste原生点

1 个答案:

答案 0 :(得分:3)

这是因为你需要只返回字符串,而Doctrine会自动用引号包装它。试试这段代码:

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

它会生成下一个SQL查询:

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