dsgk40568 2015-07-09 05:16
浏览 34
已采纳

Cakephp 3.0.8:“name”字段中的数组在patchEntity之后消失

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!

  • 写回答

2条回答 默认 最新

  • doter1995 2015-07-11 19:19
    关注

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的