Reputation: 59
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!
Upvotes: 0
Views: 645
Reputation: 60463
In the marshalling process the data is being converted based on the column type, and your columns are most probably string types, so you cannot patch arrays into them, this will result in empty strings.
Have a look at
For now, use the traits translation()
method as shown in the docs. You can do that in your saving process before or after patching your entity. It is advised that you don't use the actual column names in your form, so that you can easily patch in the request data.
Note that the original entity should hold the default language contents. So in case the default happens to be en_US
, then you should only store fr_CA
as translation.
And in order for the translated fields to be validated, you should probably use a custom translation table class, and make use of application rules!
Upvotes: 1
Reputation: 59
Here's my code and it seems to works perfectly fine!
In my view:
<div class="ingredients form large-10 medium-9 columns">
<?= $this->Form->create($ingredient) ?>
<fieldset>
<legend><?= __('Add Ingredient') ?></legend>
<?php
echo $this->Form->input('name', ['label' => __('Title')]);
foreach ($locales as $lang) {
echo $this->Form->input('locales.'.$lang.'.name', ['label' => __('Title')]);
}
echo $this->Form->input('slug');
echo $this->Form->input('season');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
I added this piece of code right before the save in my Controller:
foreach ($this->request->data['locales'] as $lang => $data) {
$ingredient->translation($lang)->set($data, ['guard' => false]);
}
You should add a way to detect if the locales in the request are valid and supported.
Upvotes: 0