( sfpropelimpersonator documentation )
Quick examples (page 2 on 5)
Basic example
Here's what you can do to retrieve an array of Article objects, assuming that your propel schema desccribes relations between Article, Author, AuthorEmail, ArticleI18n and ArticleComment.
$peer = new sfPropelObjectPeerImpersonator('Article', 'Author', 'AuthorEmail', 'ArticleI18n', 'ArticleComment'); $c = new Criteria(); $peer->addSelectColumns($c); $c->addJoin(ArticlePeer::ID, ArticleCommentPeer::ARTICLE_ID, Criteria::LEFT_JOIN); $c->addJoin(ArticlePeer::AUTHOR_ID, AuthorPeer::ID, Criteria::INNER_JOIN); $c->addJoin(AuthorPeer::ID, AuthorI18nPeer::AUTHOR_ID.' AND '.AuthorI18nPeer::CULTURE.'=\''.mysql_real_escape_string($culture).'\'', Criteria::INNER_JOIN); $c->add(AuthorPeer::VALIDATION_STATUS, true); $results = $peer->doSelect($c);
Provided that there is one or more results, the following code that retrieve relations does not issue any additional query:
$results[0]->getAuthor(); $results[0]->getAuthor()->getAuthorEmail(): $results[0]->getArticleComment();
Related objects are populated by ->doSelect() exactly as if there was an ArticlePeer::doSelectJoinAuthorAuthorEmailArticleI18nArticleComment(Criteria $c, $connection) propel generated method.
Subquery criteria example
Here's another little trick to make full use of underlying database system using propel, and cleanly using it. It's more conceptual than functionnal, but you should get it.
$c = new Criteria(); $subc = new Criteria(); $subc->addSelectColumn(xxxPeer::ID); $subc->add(....); $c->add(xxxPeer::ID, xxxPeer::ID.' IN ('.sfPropelCriteriaImpersonator::getSql($subc).')', Criteria::CUSTOM);
I will add more examples as the plugin development will go on.
Subquery in FROM part
Thanks to Patrice Blanchardie which posted a nice Propel tip on Snipeet
(
see the original), we have now a way to do
subqueries in FROM part of the queries too.
$c = new Criteria(); $subc = new Criteria(); $subc->addSelectColumn(xxxPeer::ID); $subc->add(....); $c->addAsColumn('aliasname', '('.sfPropelCriteriaImpersonator::getSql($subc).')'); // maybe you want to use your alias for sorting? $c->addAscendingOrderByColumn($c->getColumnForAs('aliasname')); $results = xxxPeer::doSelect($c);
I18n joins
There is an annoying problem with I18n joins and propel, which is that Propel does not allow to use more than one condition in ON clause after the join. That's literally limitting SQL capabilities, but some of us like me just need to use propel, whatever limitations are, so we have to find workaround to enable again thoose powerfull lost SQL capabilities. Propel impersonator trunk now contains new methods to manage Culture, and to add SQL joins containing both the joining cause and the I18N culture in the ON clause. Yes that's a hack, but hey, aren't you used to hacks when using propel?
$c->addJoin(xxxPeer::ID, $peer->getJoinForCulture('xxxI18nPeer', 'XXX_ID'), Criteria::LEFT_JOIN);
The only requirements are to have your I18n class named like your normal class with 'I18n' postfixed (that's hardcoded for I18n relation fetching too) and to have the culture field named 'culture'. I'm open to better solutions, but hey that's lifesaving to avoid 200 queries par list page in complex admin generators.
