I have to create this query:
SELECT * FROM books AS b
LEFT JOIN
(
SELECT bu.book_id, bu.user_id
FROM books_users AS bu
WHERE bu.user_id = 5
) AS bud
ON a.id = bud.book_id
ORDER BY b.owners DESC LIMIT 2
reading doc I have do this:
$dbo = $this->Book->BooksUser->getDataSource();
$conditionsSubQuery['bu.user_id'] = 5;
$subQuery = $dbo->buildStatement(
array(
'fields' => array('bu.book_id', 'bu.user_id'),
'table' => 'books_users',
'alias' => 'bu',
'conditions' => $conditionsSubQuery,
'order' => null,
'limit' => null,
'group' => null,
),
$this->Book->BooksUser
);
$subQuery2 = 'SELECT * FROM books AS b LEFT JOIN
('.$subQuery.') AS
bu ON b.id = bu.book_id ORDER BY b.owners DESC LIMIT 2';
$subQueryExpression = $dbo->expression($subQuery2);
In the end subQueryExpression contain SQL but now I have two
questions:
1) I have write the subquery in cake style. How can avoid to write
pure SQL (see $subQuery2) and rewrite main query in "cake style"???
2) subQueryExpression is just an SQL string so it need query()
function to execute, how avoid this?
Regards
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en