I am trying to convert geometry in a PostgreSQL/PostGIS table into GeoJSON inside Editor and setting an alias for that field. In the "Getting started" documentation there is an example code (showing a MySQL function) that indicates that it's possible to use SQL functions in the fields.
The field I'm creating looks like the following:
Field::inst( 'ST_AsGeoJSON(ST_Transform(ST_Centroid(files_info.geom), 4326))', 'geometry' )
->getFormatter( function($val, $data, $opts) { return json_decode($val, true); } ) // server to client
->set(Field::SET_NONE),
However, Editor doesn't see the alias in a field with a function.
Using Editor's debug()
mode, I can inspect the SQL that it's generated. Instead of using the geometry
alias for the function that converts geometry to GeoJSON, it is using the function as as the alias! Thus, DataTables cannot find the data: "geometry"
property in the data that's returned from the database.
This is the query Editor is trying:
SELECT wishlists_items.id as \"wishlists_items.id\", ST_AsGeoJSON(ST_Transform(ST_Centroid(wishlists_items.geom), 4326)) as \"ST_AsGeoJSON(ST_Transform(ST_Centroid(wishlists_items.geom), 4326))\" FROM wishlists_items ORDER BY wishlists_items.created_at desc LIMIT 10
I tried using an alias on a simpler function:
Field::inst( 'extract(year from wishlists_items.created_at) AS year' )
->set( false ),
And the problem in Editor's resulting SQL is still ignoring the year
alias:
SELECT wishlists_items.id as \"wishlists_items.id\", extract(year from wishlists_items.created_at) as \"extract(year from wishlists_items.created_at)\" FROM wishlists_items ORDER BY wishlists_items.created_at desc LIMIT 10
I have found relevant code in the Query.php
and Field.php
files, and I tried tinkering with _dbField()
and _buildField()
functions, but I couldn't get the alias to be recognized when there's a function in the field name.
Is there a bug?