Yii\Base\InvalidArgumentException: Malformed UTF-8 Characters, Possibly Incorrectly Encoded in C:\wwwroot\object\src\vendor\yiisoft\yii2\helpers\basejson.php:147
1. View the response data in the interface, and the reasoning is that the checkin_location_coordinates in the response field is the point type of mysql. as shown in Figure 1
2. Decide to adjust the query sql, use mysql spatial functions (such as st_x and st_y) to extract the latitude and longitude, and avoid direct manipulation of the point field. With the addSelect() method, you can add longitude and latitude fields on the basis of querying all fields. Then unset drop the checkin_location_coordinates field.
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. However, this scheme needs to be adjusted in all the queries of this model. The workload exceeded expectations. You need to automatically add queries CHECKIN_LOCATION_LONGITUDE, CHECKIN_LOCATION_LATITUDE in the model file. Then unset(checkin_location_coordinates). Found that the fields() method was not executed. In the end, it is still only in the controller unset($form[‘checkin_location_coordinates’]);. Decided to still use the scheme of the second step.
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. In the end, it was decided to give up the use of the POINT field type. Adjust to checkin_location_longitude decimal(9,6), checkin_location_latitude decimal(9,6)
