我有SilverStripe项目(3.1.12 cms和框架)。
我使用silverstripe-translatable
扩展cms为了翻译我的网站上的东西。
我已经创建了两个页面类型:ProductPage
和CategoryPage
下面是我在管理面板中创建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);
...
这里我在ProductPage
中设置了它和CategoryPage
之间的关系
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 %>
一切正常(产品显示在正确的类别中),直到我将区域设置更改为另一个(例如从英语更改为拉脱维亚语或俄语)。
发生这种情况是因为我没有设置在非默认区域设置中哪个产品(s)会出现在哪个类别(s)中,因此,实际问题是:我如何在非默认区域设置中访问default_locale
的关系?
注:我有一个工作网站,为什么我想实现这一点,是因为产品和类别的数量(有很多,所以编辑每一个单一的产品和类别关系在每一个地区,我有将是愚蠢的)
提前感谢!
过去我在布局/模板中调用$Master.RelationName
,这可能有效。所以你的循环应该是这样的:
<% loop $Master.AllProducts %>
$Title
<% end_loop %>