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 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)