I'm testing cakephp 3.0.8 and I have a problem. I'm trying to add new ingredients in multiple languages at the same time and for that I need my form to have an array inside the "name" and the "slug" field containing the language and the value in that language for the i18n table in the database. But after the patchEntity before the save, the array disappear and I dont understand why.
What I want:
[
'name' => [
'en_US' => 'Title',
'fr_CA' => 'Titre'
],
'slug' => [
'en_US' => 'Slug',
'fr_CA' => 'Slug Fr'
],
'season' => ''
]
What I have after patchEntity:
[
'name' => '',
'slug' => '',
'season' => ''
]
In my IngredientController.php
public function add()
{
$ingredient = $this->Ingredients->newEntity();
if ($this->request->is('post')) {
debug($this->request->data);
$ingredient = $this->Ingredients->patchEntity($ingredient, $this->request->data);
$ingredient->locale = Configure::read('Config.locales');
debug($ingredient);die();
if ($this->Ingredients->save($ingredient)) {
$this->Flash->success(__('The ingredient has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The ingredient could not be saved. Please, try again.'));
}
}
$this->set(compact('ingredient', 'recipes'));
$this->set('_serialize', ['ingredient']);
}
In my Ingredient.php entity
protected $_accessible = [
'name' => true,
'slug' => true,
'season' => true,
'recipe_count' => false,
'recipes' => false,
'*' => false
];
In my IngredientsTable.php
public function validationDefault(Validator $validator)
{
$validator
->requirePresence('name', 'create')
->notEmpty('name');
$validator
->allowEmpty('slug');
$validator
->allowEmpty('season');
return $validator;
}
And finally in my add.ctp view
<div class="ingredients form large-10 medium-9 columns">
<?= $this->Form->create($ingredient) ?>
<fieldset>
<legend><?= __('Add Ingredient') ?></legend>
<?php
foreach ($locales as $lang) {
echo $this->Form->input('name.'.$lang, ['label' => 'Title ('.$lang.')']);
echo $this->Form->input('slug.'.$lang, ['label' => 'Slug ('.$lang.')']);
}
echo $this->Form->input('season');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
CakePHP generated all the code with the console except for the add.ctp that I modified a bit to have an array inside the "name" and "slug" fields. The var $locales only contain an array of the locales (en_US and fr_CA)
Thank you!