【问题标题】:How to get the object with its translation using getRoute in Symfony?如何在 Symfony 中使用 getRoute 获取对象及其翻译?
【发布时间】:2010-02-11 18:46:19
【问题描述】:

我有几个带有翻译的模型。当我加载时

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

例如,它为我提供了 Tour Object。但是,它不加入 tour_translation 表;所以当我尝试获得它的标题后; symfony 进行另一个 sql 查询。

我如何覆盖某些东西,所以在 Tour 模型中,当我请求对象时,它会返回对象及其在当前文化中的翻译。

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

我知道我可以执行以下操作,但我更喜欢第一个选项,因为它更透明和优雅:

$this->tour = Tour::getTour($request->getParameter('id'), $lang);

谢谢!

【问题讨论】:

    标签: routing model symfony1 translation


    【解决方案1】:

    您需要在路由定义中指定通过methodoption 检索对象时使用的方法:

    my_route
      url: /tour/:id
      options:
        model: Tour
        type: object
        method: getTourForRoute
    

    (为简洁起见跳过了params部分)

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

    public function getTourForRoute($parameters)
    {
      return self::getTour($parameters['id']);
    }
    

    最后说明:此选项仅在您使用sfDoctrineRoutesfPropelRoute 时可用:-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-19
      • 2015-03-17
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 2015-01-20
      • 1970-01-01
      • 2021-12-12
      相关资源
      最近更新 更多