![]() |
|
Snippets |
|
complement complex SQL in SYMFONY:
Example 1: SQL to be implemented:
SELECT b.id FROM article_mark a RIGHT JOIN article b ON a.article_id = b.id ORDER BY a.mark DESC,b.CREATED_AT DESC
Symfony implementation:
$c=new Criteria(); $c->addAlias('a', 'article_mark'); //!!!if not using alias of table, the generated sql is not correct $c->addAlias('b', 'article'); $c->addSelectColumn('b.id'); $c->addSelectColumn('a.article_id'); //!!!if one table has no column added, there's no table name after FROM clause;; actually this column is not what I need $c->addDescendingOrderByColumn('a.mark'); $c->addDescendingOrderByColumn('b.CREATED_AT'); $c->addJoin('a.ARTICLE_ID','b.ID','RIGHT JOIN'); $art_marks=ArticlePeer::doSelectRS($c); //!!! doSelect or doSelectOne can't be used
By this way, $art_marks is a MySQL recordset, using $art_marks[0] get value of column b.id;
Example 2: SQL to be implemented:
select SUM(score) from bury group by article_id having article_id=@ARTICLE_ID
Symfony implementation:
$c=new Criteria(); $c->addSelectColumn('SUM(score) as A'); //actually 'as A' has no use $c->addSelectColumn(BuryPeer::SCORE); //!! without this, no table name in generated sql; actually this column is not what I need $c->addGroupByColumn('article_id'); $crit=$c->getNewCriterion(BuryPeer::ARTICLE_ID,$article->getId()); $c->addHaving($crit); $buries=BuryPeer::doSelectRS($c); //only MySQL recordset can be used