I added the TranslateBehavior in AcosTable class with the ShadowStrategy and created the acos_translations table, when I save the records in acos_translations table the lft and rght fields in acos table are modified with bizarre values and in acos_translations these two fields have also bizarre values, this is the code for translate action:
public function translate()
{
$aco = $this->MyPermissions->Acos->newEmptyEntity();
if ($this->request->is('post')) {
$acos = $this->MyPermissions->Acos->newEntities($this->request->getData('_translations.es'));
$guardar = false;
foreach($acos as $key => $value){
if(!empty($this->request->getData('_translations.es.'.$key.'.alias'))){
$value->_locale = 'es';
$guardar = $this->MyPermissions->Acos->save($value);
if(!$guardar) break;
}
}
if($guardar){
$this->Flash->success(__('Los permisos han sido traducidos.'));
return $this->redirect(['controller' => 'Users', 'action' => 'login']);
}
$this->Flash->error(__('Los permisos no pudieron ser traducidos. Por favor, intente nuevamente.'));
}
$parent_id = $this->MyPermissions->Acos->find('all')->select('Acos.id')->where(['alias in' => $this->translations]);
$acos = $this->MyPermissions->Acos->find('all')->where(['or' => ['alias in' => $this->translations, 'parent_id in' => $parent_id, 'parent_id is' => null]]);
$this->set(compact('aco', 'acos'));
}
And this is the code for translate view:
<?php
/**
* @var \App\View\AppView $this
* @var \App\Model\Entity\MyPermission[]|\Cake\Collection\CollectionInterface $myPermissions
*/
?>
<div class="myPermissions translate content">
<h3><?= __('Traducir permisos') ?></h3>
<?= $this->Form->create($aco) ?>
<div class="table-responsive">
<table>
<tr>
<th><?= __('Padre') ?></th>
<th><?= __('Alias') ?></th>
<th><?= __('Izquierda') ?></th>
<th><?= __('Derecha') ?></th>
<th><?= __('Traducción') ?></th>
</tr>
<?php
$i = 0;
foreach ($acos as $value) :
?>
<tr>
<td><?= h($value->parent_id) ?></td>
<td><?= h($value->alias) ?></td>
<td><?= h($value->lft) ?></td>
<td><?= h($value->rght) ?></td>
<td>
<?= $this->Form->control('_translations.es.'.$i.'.alias', ['label' => false]) ?>
<?= $this->Form->hidden('_translations.es.'.$i.'.id', ['value' => $value->id]) ?>
<?= $this->Form->hidden('_translations.es.'.$i.'.parent_id', ['value' => $value->parent_id]) ?>
<?= $this->Form->hidden('_translations.es.'.$i.'.model', ['value' => $value->model]) ?>
<?= $this->Form->hidden('_translations.es.'.$i.'.foreign_key', ['value' => $value->foreign_key]) ?>
<?= $this->Form->hidden('_translations.es.'.$i.'.lft', ['value' => $value->lft]) ?>
<?= $this->Form->hidden('_translations.es.'.$i.'.rght', ['value' => $value->rght]) ?>
</td>
</tr>
<?php
$i++;
endforeach;
?>
</table>
</div>
<?= $this->Form->button('Guardar') ?>
<?= $this->Form->end() ?>
</div>
How can I avoid the modification of acos table and saving the correct lft and rght values in acos_translations?