yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

[BUG] Some PostGIS functions can't be returned with Query #8918

Open bartek5186 opened 9 years ago

bartek5186 commented 9 years ago

PostgreSQL PostGIS Query and other ActiveRecord methods don't work for example for Query:: can't return anything when I use functions like st_x, st_y with geometry.. the returned row don't produce column name "lon".

$query = Core::find(); $query->select(new Expression("st_x(location::geometry) as lon"))->where("1=1"); // or $query->select("st_x(location::geometry) as lon")->where("1=1"); $query->all(); This dont emit any answer with lon parameter, but should work...

When I use a createCommand with setSql, everythings works fine.. (but it's not good option for the longer queries, that i need to prepare) In this case "lon" is properly returned but it worked only with createCommand method.. Yii::$app->db->createCommand()->setSql('SELECT st_y(location::geometry) as lon FROM core WHERE 1 = 1')->query(); This emit answer with lon parameter computed to for example "4.99212"

In db location field has geometry type with value for exampe: 0101000020E610000000000000000008400000000000000840

lynicidn commented 9 years ago

asArray()

cebe commented 9 years ago

You have to pass an array to select for this to work:

$query->select(["st_x(location::geometry) as lon"]);

check the docs for more details:

http://www.yiiframework.com/doc-2.0/yii-db-query.html#select%28%29-detail Note that if you are selecting an expression like CONCAT(first_name, ' ', last_name), you should use an array to specify the columns. Otherwise, the expression may be incorrectly split into several parts.

bartek5186 commented 9 years ago

I have tested it with Array too on beginning. Its not working $query->select(["st_x(location::geometry) as lon"]); Thanks for adding it as test :+1:

There are't errors, just not created lon column.