How to get the object with its translation using getRoute in Symfony?

天涯浪子 提交于 2019-12-12 05:27:12

问题


I have several models with translations. When I load

$this->tour = $this->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!


回答1:


You need to specify in your route definition what method to use when retrieving the object through the methodoption:

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 :-)



来源:https://stackoverflow.com/questions/2246824/how-to-get-the-object-with-its-translation-using-getroute-in-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!