Simple executeQuery() question

Hi there,

I'm new to Phalcon and am just playing around with the framework trying to understand it better. I've run into an issue which seems like something ridiculously simple but I havent been able to figure it out. See the code below. Any idea why the one piece of code would work but the other wouldnt?

Thanks so much for any help!

$order = "views desc";

//This doesn't work - results are not sorted
$phql = "SELECT * FROM Posts ORDER BY :ord: limit 10";
$videos = $app->modelsManager->executeQuery($phql, array(
    'ord' => $order
));

//This works! Results are sorted as expected
$phql = "SELECT * FROM Video ORDER BY " . $order . " limit 10";
$videos = $app->modelsManager->executeQuery($phql);


19.6k

I think that it is caused of binding it like a parameter (in the query, I'm nearly sure it will write ORDER BY 'column', with the '), and you want a column name, not a string.

Try setting to true phqlLiterals (don't know if the code below is correct):

Phalcon\Mvc\Model::setup(array('phqlLiterals' => true));


2.4k

Thanks for the response!

When I set phqlLiterals to true I get nothing changes. When I set it to false i get:

Uncaught exception 'Phalcon\Mvc\Model\Exception' with message 'Literals are disabled in PHQL statements'

I guess by default phqlLiterals are already true.



19.6k

Yeah, I've readed that false will print you that message.

I think you'll need to do with the second form you try, 'cause of what I said of the '. Until Phalcon says another thing.



84.6k
edited Aug '14

When you're doing this:

$order = "views desc";

//This doesn't work - results are not sorted
$phql = "SELECT * FROM Posts ORDER BY :ord: limit 10";
$videos = $app->modelsManager->executeQuery($phql, array(
    'ord' => $order,
));

You're actually doing:

$phql = "SELECT * FROM Posts ORDER BY 'views desc' limit 10"; // check the single quotes
$videos = $app->modelsManager->executeQuery($phql);