Sunday, March 1, 2009

subqueries with criteria

subqueries with criteria


I haven't found a way to use subqueries as alias with criteria in the book.



Here is a small piece of code that works very well:



the subquery is in an alias defined by Criteria::addAsColumn($alias, $expr)



it can be used to order the result, or in case you have to deal with foreign keys, etc.



$c = new Criteria();

 
$c->addAsColumn('brandname', '(SELECT brand.name FROM brand WHERE brand.id=brand_id)');
 
$c->addAscendingOrderByColumn($c->getColumnForAs('brandname'));

 
$this->products = ProductPeer::doSelect($c);
 


Product has a brand_id that references the id key of the brand table that contains the 'name' field which is used to order the result.

0 comments:

Post a Comment