如何在Symfony中使用getRoute获取带有翻译的对象? [英] How to get the object with its translation using getRoute in Symfony?

查看:87
本文介绍了如何在Symfony中使用getRoute获取带有翻译的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几种翻译模型.当我加载

I have several models with translations. When I load

$this->tour = $this->getRoute()->getObject();

例如,它为我提供了游览对象.但是,它没有加入tour_translation表中.所以当我尝试获得它的标题时; symfony进行另一个sql查询.

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.

如何覆盖某些内容,因此在Tour模型中,当我索要对象时,它将返回对象及其当前文化中的译文.

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.

我一直在查看 sfObjectRoute类,看看我是否可以覆盖任何方法,但现在不确定

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

谢谢!

推荐答案

您需要在路由定义中指定通过method选项检索对象时使用哪种方法:

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部分)

(params section skipped for brevity sake)

请注意,该方法将不会直接将id作为参数接收,而是将参数数组传递给路由,因此您将编写这样的方法:

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']);
}

最后的注释:仅当您使用sfDoctrineRoutesfPropelRoute时才可用:-)

Final note: this option is only available if you use either a sfDoctrineRoute or a sfPropelRoute :-)

这篇关于如何在Symfony中使用getRoute获取带有翻译的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆