CakePHP 3.6翻译并包含

CakePHP 3.6翻译并包含,cakephp,translate,contain,Cakephp,Translate,Contain,我需要从一个显示所有翻译的记录中构建一个视图,并且我还希望将显示关联模型中附加记录的表添加到该特定记录中。关联的模型不需要翻译,只需以当前语言显示即可。 是否有一种方法可以将“包含”与->查找(“翻译”)->结合起来? 或者最好的做法是什么 f、 一个团队有很多角色。因此,我想向该组显示Groups.name的所有翻译,以及属于该组的所有角色的列表。 现在我用不同的发现做了这件事: public function view($id = null) { $group = $this-&g

我需要从一个显示所有翻译的记录中构建一个视图,并且我还希望将显示关联模型中附加记录的表添加到该特定记录中。关联的模型不需要翻译,只需以当前语言显示即可。 是否有一种方法可以将“包含”与->查找(“翻译”)->结合起来? 或者最好的做法是什么

f、 一个团队有很多角色。因此,我想向该组显示Groups.name的所有翻译,以及属于该组的所有角色的列表。

现在我用不同的发现做了这件事:

public function view($id = null)
{
    $group = $this->Groups->find('translations')->where(['Groups.id' => $id])->first();
    $this->set('group', $group);
    // related Roles
    $related_roles = $this->Groups->Roles->find('all', [
        'conditions' => ['Roles.group_id' => $id]
    ]);
    $this->set('related_roles', $related_roles);
}
但是我想知道是否不可能在1个find中组合这个,是否有可能在find('translations')中使用某种contain()。

我这样解决它:

$group = $this->Groups
            ->find('translations')
            ->where(['Groups.id' => $id])
            ->contain(['Roles', 'Users'])
            ->first();
$group = $this->Groups
            ->find('translations')
            ->where(['Groups.id' => $id])
            ->first();
$group->contain(['Roles', 'Users']);
在我尝试之前,如手册中所示:

$group = $this->Groups
            ->find('translations')
            ->where(['Groups.id' => $id])
            ->contain(['Roles', 'Users'])
            ->first();
$group = $this->Groups
            ->find('translations')
            ->where(['Groups.id' => $id])
            ->first();
$group->contain(['Roles', 'Users']);

但无论出于什么原因,这对我都不起作用。

我们已经用这个解决了这个问题,也许这会有所帮助

为翻译创建一个单独的表,即组

现在在Entity/Group.php中添加行

protected $_accessible = [
    //other columns
    'name_translation' => true,
    '_i18n' => true,
    '_translations' => true
];
现在在GroupsTable.php中,添加行为

$this->addBehavior('Translate', [
        'fields' => ['name'],
        'translationTable' => 'GroupsI18n'
    ]);
现在在GroupsController.php中

$group = $this->Groups->get($id, [
        'contain' => ['Roles', 'Groups_name_translation', 'GroupsI18n']
    ]);

您可能需要添加更多的细节,以了解您到底想要实现什么(例如,现有数据和结果数据的示例),以及您到底面临什么问题。我假设您在第一个查询中使用
contain()
时遇到了问题?我仍然不确定您想要的结果是什么,从最初的语句判断,您希望容器不被翻译,但是更新读起来好像您可能希望容器也被翻译?只有主记录(在屏幕截图中,“Administrators”组需要所有翻译。相关记录只能以一种语言显示(所选语言)。因此,您可以看到上面的代码运行良好。我只想知道是否有更好的(蛋糕式)方法。简言之,您可以将contain()与find('translations')一起使用吗是的,你可以,你试过了吗?是的,我试过了,但是没有用。你能告诉我如何为上午代码编写它吗?