学说问题:无法从发送到几何字段的数据中获取几何对象



我遵循本指南将point类型添加到学说。

这就是我在实体中定义坐标字段的方式:

/**
 * @ORMColumn(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: VinyPointType
        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: 'AppEntity'
                alias: App

结果DB列定义:

`coordinates` point DEFAULT NULL,

结果我得到:

用params [" test"," point(-73.976683 40.760381((执行'插入' geo_point( addresscoordinates('inseert时发生了例外。

sqlstate [22003]:数值超出范围:1416无法获得几何形状 从您发送到几何字段的数据中的对象

最终查询:

插入geo_point(addresscoordinates(值('test','point(-73.976683 40.760381('(;

您已发布的学说配置是错误的。应该是:

#doctrine.yaml
doctrine:
    dbal:
        types: ### Use types instead of mapping_types
            point: App...GeoPointType

您可以知道映射是一个问题,因为您生成的SQL:

INSERT INTO geo_point (address, coordinates) VALUES ('Test', 'POINT(-73.976683 40.760381)');

mySQL本身不明白点。它需要包裹在PointFromText中。此包装是通过以下方式完成的:

    // class GeoPointType
    public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
    {
        return sprintf('PointFromText(%s)', $sqlExpr);
    }

显然没有被称为。

这是一个有效的例子。

最新更新