Yii2 REST DB 几何字段序列化

Yii2 REST DB Geometry field serialization

我在 mySQL 中有一个 table,其中包含 GEOMETRY 和 POLYGON 类型的字段。还有 Yii2 RESTful 应用程序。这是一个标准的 ActiveController:

<?php

namespace backend\modules\v1\controllers;

use common\models\Geo;

class GeoController extends ActiveController
{
    public $modelClass = 'common\models\Geo';
}

当我的 table 中有任何包含空间数据的记录时 JSON 响应中断并显示消息:

code: 5
file: "/vendor/yiisoft/yii2/helpers/BaseJson.php"
line: 120
message: "Malformed UTF-8 characters, possibly incorrectly encoded."
name: "Exception"

我应该编写自定义 json 序列化程序还是以某种方式覆盖 ActiveProvider select,比如 "AsText(geometry_field_name)"?

我认为您应该更改 JSON (http://www.tutorialspoint.com/json/json_data_types.htm) 支持的字段类型。

例如,如果您使用 MySQL 类型为 CHAR 的字段,您将得到相同的错误

基本上,通过使用 SQL 命令 AsText(geomery_field_name) as geometry_text.

添加额外的列来扩展 ActiveQuery class 解决了这个问题