How to get the object with its translation using getRoute in Symfony?
I have several models with translations. When I load
$this->tour = $th开发者_StackOverflowis->getRoute()->getObject();
por example, it gets me the Tour Object. However, it doesn't join to the tour_translation table; so when after i try to get it's title; symfony makes another sql query.
How I can override something, so in the Tour model when I ask for the object, it returns me the object with its translation in the current culture.
I've been looking at the sfObjectRoute class to see if I can override any method, but I'm not sure right now
I know I can do the following, but I prefer the first option as it's more transparent and elegant:
$this->tour = Tour::getTour($request->getParameter('id'), $lang);
thanks!
You need to specify in your route definition what method to use when retrieving the object through the method
option:
my_route
url: /tour/:id
options:
model: Tour
type: object
method: getTourForRoute
(params
section skipped for brevity sake)
Be aware that the method will not receive the id
directly as a parameter but an array of parameters passed to the route, thus you would write a method like that:
public function getTourForRoute($parameters)
{
return self::getTour($parameters['id']);
}
Final note: this option is only available if you use either a sfDoctrineRoute
or a sfPropelRoute
:-)
精彩评论