A Finder Smarter Than Propel Getters
The sfPropelFinder symfony plugin keeps getting better. The addition of two new methods, relatedTo() and findLast() make things even easier than before. Let's see it with an example.
Propel is smart enough to generate getter methods for related objects when you define a foreign key in your schema. For instance, if your schema relates Comment to Article as follows:
propel:
article:
title: varchar(255)
body: longvarchar
created_at: ~
comment:
article_id: ~
author: varchar(100)
body: longvarchar
created_at: ~
Then after you build your model, the BaseArticle class will provide a getComments() method that will facilitate the retrieval of the comments related to an existing article:
$comments = $article->getComments();
But now, what if you need to get the list of comments ordered from the last posted to the first posted? The generated getter method accepts a Criteria as its first parameter, so you can write:
$c = new Criteria();
$c->addDescendingOrderByColumn(CommentPeer::CREATED_AT);
$comments = $article->getComments($c);
If you want the latest comment posted on an article, you will need to make something slightly more complicated. For the sake of the example, the code appears on the same fashion as the previous ones, but you should definitely wrap it up in a method stored in the Article class.
$c = new Criteria();
$c->addDescendingOrderByColumn(CommentPeer::CREATED_AT);
$comments = $article->getComments($c);
if(isset($comments[0])
$comment = $comments[0];
}
else
{
$comment = null;
}
// Alternative way, but not really shorter
$c = new Criteria();
$c->add(CommentPeer::ARTICLE_ID, $article->getId());
$c->addDescendingOrderByColumn(CommentPeer::CREATED_AT);
$comment = CommentPeer::doSelectOne($c);
The sfPropelFinder provides an alternative, and I believe better way of doing this. The finder object can filter results related to a given object thanks to its new relatedTo() method. This method uses the schema to guess the local and foreign columns, so you don't have to pass any other argument than the related object:
$comments = sfPropelFinder::from('Comment')->
relatedTo($article)->
find();
How is that better than $article->getComments()? It is not. But for the following examples, the interest of the relatedTo() method appears more clearly:
$comments = sfPropelFinder::from('Comment')->
relatedTo($article)->
orderByCreatedAt()->
find();
$comments = sfPropelFinder::from('Comment')->
relatedTo($article)->
findLast();
findLast() is also a recent addition to the plugin. It returns a single record, the last one, based on the creation date (or on the id if there is no creation date column).
The relatedTo() method offers the same convenient way to retrieve related objects by a single method call as Propel generated getters. But since it is embedded in the sfPropelFinder class, it allows for further manipulation of the results with a very simple API.
So if you didn't give it a try yet, checkout the sfPropelFinderPlugin from the symfony Subversion repository. You will soon see how it dramatically reduces the amount of code you write in the model.
Comments(12)
