yii\base\InvalidArgumentException: Malformed UTF-8 characters, possibly incorrectly encoded in C:\wwwroot\object\src\vendor\yiisoft\yii2\helpers\BaseJson.php:147
1、查看接口中的响应数据,定位原因在于响应字段中的 checkin_location_coordinates 为 MySQL 的 point 类型。如图1
2、决定调整查询 SQL,使用 MySQL 的空间函数(如 ST_X 和 ST_Y)提取经纬度,避免直接操作 POINT 字段。通过 addSelect() 方法,可以在查询所有字段的基础上,额外添加经度和纬度字段。然后再 unset 掉 checkin_location_coordinates 字段。
SELECT * FROM `survey_forms` WHERE (`id`='1825203750690273') AND (`user_id`='1825203750689422') LIMIT 1
SELECT *, ST_X(checkin_location_coordinates) AS `checkin_location_longitude`, ST_Y(checkin_location_coordinates) AS `checkin_location_latitude` FROM `survey_forms` WHERE `id`='1825203750690273' LIMIT 1
$form = SurveyForm::find() ->select('*') // 查询所有字段 ->addSelect([ 'ST_X(checkin_location_coordinates) AS checkin_location_longitude', 'ST_Y(checkin_location_coordinates) AS checkin_location_latitude', ]) ->where([ 'id' => $id, ])->limit(1)->asArray()->one(); unset($form['checkin_location_coordinates']);
3、但是,这种方案在所有的这个模型的查询中皆要调整。工作量超出预期。需要在模型文件中实现 自动添加查询 checkin_location_longitude、checkin_location_latitude。然后 unset(checkin_location_coordinates)。发现 未执行到 fields() 方法。最后仍然只有在控制器中 unset($form[‘checkin_location_coordinates’]);。决定仍然采用第二步骤的方案。
public $checkin_location_longitude; public $checkin_location_latitude; public function afterFind() { parent::afterFind(); // 设置经度和纬度 $this->checkin_location_longitude = Yii::$app->db->createCommand("SELECT ST_X(checkin_location_coordinates) FROM {$this->tableName()} WHERE id = :id", [':id' => $this->id])->queryScalar(); $this->checkin_location_latitude = Yii::$app->db->createCommand("SELECT ST_Y(checkin_location_coordinates) FROM {$this->tableName()} WHERE id = :id", [':id' => $this->id])->queryScalar(); // 排除原始字段 unset($this->checkin_location_coordinates); } public function fields() { $fields = parent::fields(); Yii::debug('Fields before modification: ' . print_r($fields, true)); // 添加自定义字段 $fields['checkin_location_longitude'] = 'checkin_location_longitude'; $fields['checkin_location_latitude'] = 'checkin_location_latitude'; // 排除原始字段 unset($fields['checkin_location_coordinates']); Yii::debug('Fields after modification: ' . print_r($fields, true)); return $fields; }
$form = SurveyForm::find()->where([ 'id' => $id, ])->with(['fields', 'fields.diagram_image', 'fields.options', 'cover_image', 'submit_success_image'])->limit(1)->asArray()->one(); unset($form['checkin_location_coordinates']);
4、最终决定放弃使用 point 字段类型。调整为 checkin_location_longitude decimal(9,6) 、checkin_location_latitude decimal(9,6)
近期评论