【问题标题】:SilverStripe many to many translation issueSilverStripe 多对多翻译问题
【发布时间】:2015-09-17 18:34:16
【问题描述】:

我有 SilverStripe 项目(3.1.12 cms 和 framework)。

我正在使用 silverstripe-translatable 扩展 cms 来翻译我网站上的内容。

我创建了两种页面类型:ProductPageCategoryPage

这是我在管理面板中创建 UI 的代码,用于勾选产品将出现的类别(在 getCMSFields() 函数内):

ProductPage.php

...
$options = CategoryPage::get();
$cb = new CheckboxSetField(
    $name = "categories",
    $title = "Select Categories",
    $source = $options, 
    $value = $this->CategoryPage()
);
$fields->addFieldToTab("Root.Categories", $cb);
...

这是我在ProductPageCategoryPage 之间建立关系的地方

private static $belongs_many_many = array(
    'Categories' => 'CategoryPage'
);

CategoryPage 也是如此,代码如下:

<?php
class CategoryPage extends Page {
   ...
   private static $many_many = array(
    'Products' => 'ProductPage'
   );
  // This function is used to get all products
  // that belong to one or more than one category
  // (thanks to many-many relationship
  public function AllProducts() {
    return $this->Products();
  }
  ...

如果我像这样循环$AllProducts 函数:

<% loop $AllProducts %>
  $Title
<% end_loop %>

一切正常(产品显示在正确的类别中),直到我将我的区域设置更改为另一个区域(比如说从英语到拉脱维亚语或俄语)。

发生这种情况是因为我没有设置哪些产品会出现在非默认语言环境中的哪些类别中,所以,实际的问题是:我如何访问default_locale's非默认语言环境中的关系?

附:我有一个工作网站,为什么我要实现这个目标,是因为产品和类别的数量(有很多,所以在我拥有的每个语言环境中编辑每个产品和类别关系会很愚蠢)

提前致谢!

【问题讨论】:

    标签: php frameworks silverstripe


    【解决方案1】:

    过去我在布局/模板中调用了$Master.RelationName,这可能有效。所以你的循环会是这样的:

    <% loop $Master.AllProducts %>
        $Title
    <% end_loop %>
    

    【讨论】:

    • 我试过这个选项,但没有运气。现在在玩文档。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    相关资源
    最近更新 更多