G
G
Gildars2016-07-11 23:19:32
Phalcon
Gildars, 2016-07-11 23:19:32

How to execute a subquery using PHQL?

There is a request like this:

SELECT Messages.*
FROM (
    SELECT MAX(id) AS 'maxid', IF (author = 'Gildars', recipient, author) AS 'user'
    FROM Messages
    WHERE author = 'Gildars' or recipient = 'Gildars'
    GROUP BY user
) AS a
INNER JOIN Messages ON a.maxid = id
ORDER BY id DESC

I need to execute this request using PHQL.
Here is how I tried to execute it:
$this->modelsManager->executeQuery("SELECT Aincrad\Models\Messages.*
FROM (
    SELECT MAX(id) AS 'maxid', IF (author = 'Gildars', recipient, author) AS 'user'
    FROM Aincrad\Models\Messages
    WHERE author = 'Gildars' or recipient = 'Gildars'
    GROUP BY user
) AS a
INNER JOIN Aincrad\Models\Messages ON a.maxid = id
ORDER BY id DESC
 ");

But everything is deaf.
Mistake
Syntax error, unexpected token (, near to ' SELECT MAX(id) AS 'maxid', IF (author = 'Gildars', recipient, author) AS 'user' FROM Aincrad\Models\Messages WHERE author = 'Gildars' or recipient = 'Gildars' GROUP BY user ) AS a INNER JOIN Aincrad\Models\Messages ON a.maxid = id ORDER BY id DESC ', when parsing: SELECT Aincrad\Models\Messages.* FROM ( SELECT MAX(id) AS 'maxid', IF (author = 'Gildars', recipient, author) AS 'user' FROM Aincrad\Models\Messages WHERE author = 'Gildars' or recipient = 'Gildars' GROUP BY user ) AS a INNER JOIN Aincrad\Models\Messages ON a.maxid = id ORDER BY id DESC (314)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yeah, 2016-07-19
@Gildars

Subqueries in the from clause aren't supported, ho...
This, by the way, is quite logical: just ask yourself the question, how does Phalcon know what type of objects to create for your FROM?
If rewriting SQL is not possible, then use raw-sql with custom hydration.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question