【发布时间】:2019-05-22 15:30:37
【问题描述】:
我跟着this guide把point类型加到Doctrine里了。
这就是我在实体中定义坐标字段的方式:
/**
* @ORM\Column(type="point", nullable=true)
*/
private $coordinates;
这就是我试图保存实体的方式:
$obj = new GeoPoint();
$obj->setAddress('Test');
$obj->setCoordinates(new Point(40.7603807, -73.9766831));
$manager->persist($obj);
$manager->flush();
我的配置:
doctrine:
dbal:
types:
point: Viny\PointType
driver: 'pdo_mysql'
server_version: '5.7'
charset: utf8mb4
default_table_options:
charset: utf8mb4
collate: utf8mb4_unicode_ci
url: '%env(resolve:DATABASE_URL)%'
mapping_types:
point: point
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
quote_strategy: backend_lib.orm.quote_strategy
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
结果数据库列定义:
`coordinates` point DEFAULT NULL,
结果我得到:
使用参数 ["Test", "POINT(-73.976683 40.760381)"] 执行 'INSERT INTO
geo_point(address,coordinates) VALUES (?, ?)' 时发生异常:SQLSTATE[22003]:数值超出范围:1416 无法获取几何 对象来自您发送到 GEOMETRY 字段的数据
最终查询:
插入
geo_point(address,coordinates)值('测试','POINT(-73.976683 40.760381)');
【问题讨论】:
-
将转储语句添加到 PointType::convertToDatabaseValue 并验证是否正在生成类似于 POINT(40.7603807, -73.9766831) 的内容。这将显示接线是否正确。有点奇怪,new Point() 并没有因为缺少构造函数参数而发出警告,而是离题。
-
我很确定你想在你的配置文件中使用类型:point: App\...\PointType。否则,我看不到 Doctrine 将如何知道使用您的自定义 PointType。 symfony.com/doc/current/reference/configuration/…
-
我已经用 INSERT 语句更新了错误信息。另外,
PointType::convertToDatabaseValue()的输出是string(27) "POINT(-73.976683 40.760381)"(坐标之间没有逗号)
标签: symfony geometry doctrine geospatial symfony4