![]() |
|
Snippets |
|
Let say you want to write a sub-select, and have a Propel object returned.
As an example, lets generate the following SQL:
SELECT orders.ID, orders.TYPE, orders.STATUS FROM orders WHERE orders.STATUS IN ( SELECT STATUS.NAME FROM STATUS WHERE STATUS.ORDER_TYPE = 'purchase' )
Propel does not natively handle this sort of SQL. There are however two ways to create this SQL or to get this data set and have Propel objects returned.
$c = new Criteria(); $subSelect = "orders.STATUS IN ( SELECT status.NAME FROM status WHERE status.ORDER_TYPE = 'purchase' )"; $c->add(StatusPeer.STATUS, $subSelect, Criteria::CUSTOM); $orders = StatusPeer::doSelect($c);
Example of the rewritten SQL:
SELECT orders.ID, orders.TYPE, orders.STATUS FROM orders, STATUS WHERE orders.STATUS = STATUS.NAME AND STATUS.ORDER_TYPE = 'purchase'
This can be written as follows in your action:
$c = new Criteria(); $c->addJoin (OrderPeer::STATUS, StatusPeer::NAME); $c->add(StatusPeer.ORDER_TYPE, 'purchase'); $orders = StatusPeer::doSelect($c);
Comments on this snippet
I'm a little amazed that it is actually impossible to build a criteria object and add it to a criteria or criterion as a subselect. Logically, you'd think this would work:
I bet we can either overload the getNewCriterion function, or write a custom function that can allow this functionality. I think I'll work on it soon.
I want to do a subselect in the FROM clause .... I suspect you can't do a CUSTOM on the addJoin method? The API doens't say it at least.
There is a small typo in the 'Way 1: Use of Criteria::CUSTOM' code: $c->add(StatusPeer.STATUS, $subSelect, Criteria::CUSTOM); SHOULD BE $c->add(StatusPeer::STATUS, $subSelect, Criteria::CUSTOM);
I managed to use this 'way' for a subquery where I needed to find the files that were not already associated with a particular course session: public function executeFiletosessions() { $primary_group_id = //retrieve value $sessionid = //retrieve value $c = new Criteria(); $c->clearSelectColumns(); $c->addAscendingOrderByColumn(FilePeer::FILE_NAME); $c->addJoin(FilePeer::ID,FileToSessionPeer::FILE_ID, Criteria::LEFT_JOIN); $c->add(FilePeer::SF_GUARD_GROUP_ID, $primary_group_id); $subSelect = "file.id NOT IN ( SELECT file_id FROM file_to_session WHERE file_to_session.course_session_id=$sessionid)"; $c->addAnd(FileToSessionPeer::COURSE_SESSION_ID, $subSelect, Criteria::CUSTOM);
$this->files = FilePeer::doSelect($c); }
I'm always amazed when I get something to work correctly!
The Way 1 is incorrectly formatted.
It should be: $c->add(StatusPeer::STATUS, $subSelect, Criteria::CUSTOM);
and not: $c->add(StatusPeer.STATUS, $subSelect, Criteria::CUSTOM);