yii2 icon indicating copy to clipboard operation
yii2 copied to clipboard

ActiveController ( REST api) and POINT column

Open DmitryBay opened this issue 5 years ago • 5 comments

When I used POINT column in DB (MySQL) and try to get GET:ITEMS via REST ActiveController, I get next error: "name": "Exception", "message": "Malformed UTF-8 characters, possibly incorrectly encoded.",

What steps will reproduce the problem?

Field - 'lnglat'=>'point not null',

Additional info

Q A
Yii version 2.0.39-dev
PHP version 7.2.33
DB MySQL - image: mysql:5.7

DmitryBay avatar Nov 03 '20 17:11 DmitryBay

Thanks for posting in our issue tracker. In order to properly assist you, we need additional information:

  • When does the issue occur?
  • What do you see?
  • What was the expected result?
  • Can you supply us with a stacktrace? (optional)
  • Do you have exact code to reproduce it? Maybe a PHPUnit tests that fails? (optional)

Thanks!

This is an automated comment, triggered by adding the label status:need more info.

yii-bot avatar Nov 03 '20 23:11 yii-bot

When does the issue occur? in yii\rest\ActiveController, on GET request. when one of DB field is POINT (NOT NULL)

What do you see? next response: { "name": "Exception", "message": "Malformed UTF-8 characters, possibly incorrectly encoded.", "code": 5, "type": "yii\\base\\InvalidArgumentException", "file": "/app/vendor/yiisoft/yii2/helpers/BaseJson.php", "line": 133, "stack-trace": [ "#0 /app/vendor/yiisoft/yii2/helpers/BaseJson.php(67): yii\\helpers\\BaseJson::handleJsonError(5)", "#1 /app/vendor/yiisoft/yii2/web/JsonResponseFormatter.php(119): yii\\helpers\\BaseJson::encode(Array, 320)", "#2 /app/vendor/yiisoft/yii2/web/JsonResponseFormatter.php(104): yii\\web\\JsonResponseFormatter->formatJson(Object(yii\\web\\Response))", "#3 /app/vendor/yiisoft/yii2/web/Response.php(1079): yii\\web\\JsonResponseFormatter->format(Object(yii\\web\\Response))", "#4 /app/vendor/yiisoft/yii2/web/Response.php(337): yii\\web\\Response->prepare()", "#5 /app/vendor/yiisoft/yii2/base/Application.php(392): yii\\web\\Response->send()", "#6 /app/frontend/web/index.php(17): yii\\base\\Application->run()", "#7 {main}" ] }

What was the expected result? Array of items. When I delete in Model::fields() 'POINT' column, I get good result with list of items.

DmitryBay avatar Nov 04 '20 05:11 DmitryBay

Would you please provide table schema, a model and some code from where you make a query?

samdark avatar Nov 04 '20 09:11 samdark

Please clone this repository, I added some notes into readme.md. https://github.com/DmitryBay/restbug

DmitryBay avatar Nov 05 '20 04:11 DmitryBay

My reading of Yii2 RESTApi, json and ActiveModel suggests that a response does not handle binary or objects returned by the database, unless the data is converted to string or integer by SQL script. As far as I can tell, the Yii2 api's do not support geoJSON. (Though someone will correct me).

Thus an action in your controller to get the string values will provide data that can be converted to json. No doubt there are better methods to handle similar problems.

public function actionTest()
    {
        $query = new \yii\db\Query;
        $query->select('id, name, ST_X(latlng) as lat, ST_Y(latlng) as lng')->from('place');
        $rows = $query->all();
        return array('status' => true, 'data' => $rows);
    }

There are a couple of extensions that may help: Yii2 Geometry Extension and yii2-spacial

mbunyan avatar Feb 03 '21 22:02 mbunyan