0

I need to build a view from a record showing all translations and I also want to add tables showing the attached records from associated models to that specific record. The associated models don't need to be translated they can just be shown in the current language. Is there a way to combine 'Contain' with ->find('translations')->? Or what would be the best practice to do this?

f.e. a Group has many Roles. So I want to show the group with all the translations for the Groups.name and also a list of all Roles belonging to this Group. enter image description here

Now I did it with separate finds:

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

But I wonder if it's not possible to combine this in 1 find, if there is a possibility to use some kind of contain() with find('translations').

Lucky
  • 95
  • 1
  • 7
  • You may want to add a little more details as to what exactly you are trying to achieve (an example of the existing and the resulting data for example), and what exactly the problem is that you are facing. – ndm Aug 27 '18 at 15:37
  • I assume that you've encountered a problem when using `contain()` in the first query? I'm still not sure what you want the outcome to be, judging from the initial statement you want the containments to be untranslated, but the update reads as if you maybe want the containments to be retrieved translated too? – ndm Aug 27 '18 at 18:36
  • Only the main record (in the screenshot the 'Administrators' Group needs to have all translations. The related records can be shown in only 1 language (the selected language. So the code you can see above works well I only want to know if there's maybe a better (Cake-) way to do it. So in short: Can you use contain() with find('translations')? – Lucky Aug 27 '18 at 18:44
  • Yes you can, have you tried it? – ndm Aug 27 '18 at 19:21
  • Yes, i tried but it didn’t work. Can you tell me how to write it for the a.m. code? – Lucky Aug 27 '18 at 19:24
  • There's nothing special to it, you'd use like you'd do with any other query. If there's a problem, it would be good if you'd elaborate on that, that's what I was trying to get to. – ndm Aug 27 '18 at 19:41

2 Answers2

1

We have solved this problem by using this, may be this can help

Create a separate table for translations i.e, groups_i18n https://book.cakephp.org/3.0/en/orm/behaviors/translate.html#using-a-separate-translations-table

now in add lines in Entity/Group.php

protected $_accessible = [
    //other columns
    'name_translation' => true,
    '_i18n' => true,
    '_translations' => true
];

now in GroupsTable.php, add behavior

$this->addBehavior('Translate', [
        'fields' => ['name'],
        'translationTable' => 'GroupsI18n'
    ]);

now in GroupsController.php

$group = $this->Groups->get($id, [
        'contain' => ['Roles', 'Groups_name_translation', 'GroupsI18n']
    ]);
Ved
  • 716
  • 4
  • 8
0

I solved it like this:

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

Before I tried is as in the manual like this:

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

But for whatever reason that wasn't working for me.

Lucky
  • 95
  • 1
  • 7