如何在CakePHP ORM中使用自定义数据库类型?保留并获取空间数据

时间:2016-01-15 17:14:01

标签: orm cakephp-3.0

我试着说长话短说:

MySQL有一段时间的空间Point数据类型。要插入数据,我们需要使用INSERT (...) VALUES (POINT(lon lat) ...)的表达式。要在CakeORM中执行此操作,我们需要将我们的给定属性设置为new QueryExpression("POINT($lon,$lat)");,并且CakePHP将在保存实体时处理数据绑定。

现在我要做的是拥有GeoJSON Point类型的属性(字段)的实体,它对应于类型Point的Mysql列,以及存储和获取实体的能力db

我以这种方式使用beforeSave回调:

public function beforeSave(Event $event, \Cake\Datasource\EntityInterface $entity, ArrayObject $options) {
    $coords = $entity->position->getCoordinates();
    $entity->position = new \Cake\Database\Expression\QueryExpression('POINT(' . $coords[0] . ',' . $coords[1] . ')');
}

其中$entity->position的类型为Point(附加的lib,但这可能是任何东西)。这通常有效,但如果我这样做,则会修改实体实体

$entity->position=new Point(5,10);
$table->save($position);
$entity->position // <- this will be Query expression insteed of Point(5,10);

在第三步中,我希望仍然拥有Point个对象,而不是修改后的值 - QueryExpression

我知道CakePHP应该支持自定义数据库数据类型,但它没用,因为......它只是不起作用,因为我希望它工作。这是一个GitHub问题,描述了为什么推荐(通过文档)解决方案不起作用。

https://github.com/cakephp/cakephp/issues/8041

0 个答案:

没有答案
相关问题