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 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化