Model Group By Parsing Bug
Hi,
I am trying to use a conditional group by statement and I do not think that the following is parsing properly in Phalcon 2.0.7:
$this->view->setVar('testData', \TestProject\Models\MyTestModel::find(array(
'conditions' => 'id_user = :id_user:',
'group' => 'my_test_referenced_field, IF(my_test_referenced_field IS NULL, id_pk_of_table, NULL)',
'order' => "status = 'ACTIVE' DESC, dt_created DESC",
'bind' => array(
'id_user' => $this->_user->getIdUser()
),
'bindTypes' => array(
'id_user' => Column::BIND_PARAM_INT
)
)));
As it throws the following exception. Notice that is removes the spaces in the my_test_referenced_field IS NULL as well has has an improper open bracket (without a closing bracket around the [NULL):
Internal 500 error: Scanning error before 'IF(my_test_referenced_...' when parsing: SELECT [TestProject\Models\MyTestModel].* FROM [TestProject\Models\MyTestModel] WHERE id_user = :id_user: GROUP BY [my_test_referenced_field], [IF(my_test_referenced_fieldISNULL], [id_pk_of_table], [NULL)] ORDER BY status = 'ACTIVE' DESC, dt_created DESC (264)
The following RAW SQL works as expected:
SELECT tm.* FROM my_test_model tm WHERE id_user = 1 GROUP BY my_test_referenced_field, IF(my_test_referenced_field IS NULL, id_pk_of_table, NULL) ORDER BY status = 'ACTIVE' DESC, dt_created DESC;
Please let me know if you need any additional info and thanks in advance!
ORDER BY status = 'ACTIVE' DESC ?
Yeah, that's a valid way to sort explicitly so that the status of ACTIVE is returned first and all other status's I don't care order.
At any rate, I can remove that and the group by isn't parsing properly.
Encountered same problem trying to update phalcon from 1.3 to 3.0
group by clause:
GROUP BY to_date_with_tz(game.date, '0')
parsed into:
GROUP BY to_date_with_tz(game.date, ['0')]
Got it working by switching usage from
$query->groupBy('to_date_with_tz(game.date, '0')')
to
$query->groupBy(array('to_date_with_tz(game.date, '0')'))
this is where the problem is https://github.com/phalcon/cphalcon/blob/master/phalcon/mvc/model/query/builder.zep#L1245
if given argument is string type than it splits it by coma, that can cause incorrect sql.
I think this case should be noted in documentation or split algorithm should be based on regular expression
Thank you for contributing to this issue. As it has been 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please feel free to either reopen this issue or open a new one. We will be more than happy to look at it again! You can read more here: https://blog.phalconphp.com/post/github-closing-old-issues