Hello everyone, i would like translate the data associated Metas of my Post
PostsTable
public function initialize(array $config)
{
parent::initialize($config);
[...]
$this->hasMany('Metas', [
'foreignKey' => 'post_id'
]);
$this->addBehavior('Translate', ['fields' => ['title', 'body', 'slug'], 'allowEmptyTranslations' => true]);
}
MetasTable
public function initialize(array $config)
{
parent::initialize($config);
[...]
$this->belongsTo('Posts', [
'foreignKey' => 'post_id'
]);
$this->addBehavior('Translate', ['fields' => ['meta_value'], 'allowEmptyTranslations' => true]);
}
debug($this->request->data);
'fr' => [
'title' => 'Titre en Français',
'slug' => '',
'body' => '<p>...</p>',
'online' => '0',
'category_id' => '1',
'type_id' => '2',
'metas' => [
(int) 0 => [
'meta_key' => 'picture',
'type_id' => '2',
'meta_value' => 'url de l'image en FR'
],
(int) 1 => [
'meta_key' => 'laureat_actuel',
'type_id' => '2',
'meta_value' => 'url de la photo en FR'
],
(int) 2 => [
'meta_key' => 'date',
'type_id' => '2',
'meta_value' => 'date en FR'
],
(int) 3 => [
'meta_key' => 'nom',
'type_id' => '2',
'meta_value' => 'Nom en FR'
],
(int) 4 => [
'meta_key' => 'bio',
'type_id' => '2',
'meta_value' => 'Bio en FR'
]
]
],
'gb' => [
'title' => 'Titre en GB',
'slug' => '',
'body' => '<p>...</p>',
'metas' => [
(int) 0 => [
'meta_key' => 'picture',
'type_id' => '2',
'meta_value' => 'url en GB'
],
(int) 1 => [
'meta_key' => 'laureat_actuel',
'type_id' => '2',
'meta_value' => 'photo en GB'
],
(int) 2 => [
'meta_key' => 'date',
'type_id' => '2',
'meta_value' => 'date GB'
],
(int) 3 => [
'meta_key' => 'nom',
'type_id' => '2',
'meta_value' => 'nom GB'
],
(int) 4 => [
'meta_key' => 'bio',
'type_id' => '2',
'meta_value' => 'bio GB'
]
]
]
After have post this data, the translation is good in my PostsTable, with the associated Metas, But the Metas are not translated
Here’s how I save my translations:
$post = $this->Posts->newEntity($this->request->data, ['translations' => true]);
$post = $this->Posts->patchEntity(
$post,
$this->request->data,
[
'associated' => ['Metas']
]
);
$translations = $this->request->data;
foreach ($translations as $lang => $data) {
$post->translation($lang)->set($data, ['guard' => false]);
}
$this->Posts->save($post);
If i translate the Posts : OK
If i translate the Metas : OK
But if i translate Posts->Metas : No
Do you have a trick for my request ?
Thx everyone