【问题标题】:Update a HasMany records更新 HasMany 记录
【发布时间】:2016-01-26 16:05:40
【问题描述】:

我正在将一个工作应用程序从 cakephp2 转换为 cakephp3。我正在努力获得一个更新 hasMany 记录的表单。

该应用具有以下结构:

型号:

use Cake\ORM\Table;
use Cake\Validation\Validator;

class AwardsTable extends Table
{
  public function initialize(array $config)
  {
   $this->hasMany('Levels', ['sort' => 'sort_order']);
  }
}


namespace App\Model\Entity;
use Cake\Auth\DefaultPasswordHasher;
use Cake\ORM\Entity;

class Award extends Entity
{
  protected $_accessible = [
    'name'            =>  true,
    'url'             =>  true,
    'organisation'    => true,
    'detail'          =>  true,
    'logo'            =>  true,
    'levels'          =>  true,
    'Levels'          =>  true
   ];

}

在表格中:

<?= $this->Form->input("levels.$i.name",  'label'=>false,'type'=>'text','value' => $award->name]);?>
<?= $this->Form->input("levels.$i.id", ['value' => $award->id]); ?>

控制器

$this->Awards->patchEntity($award, $this->request->data, ['associated' => ['Levels']]);         
if ($this->Awards->save($award)) {
    $this->Flash->success(__('Your Award has been saved.'));
    $this->redirect(['action' => 'index']);
}

这似乎符合此处推荐的内容:http://book.cakephp.org/3.0/en/views/helpers/form.html#associated-form-inputs

我尝试了一些大小写和复数形式的变体。奖励数据正确保存,但相关级别数据未保存。

我缺少什么来保存 has_many 关联数据?

编辑:提交的示例数据数组:

    2016-01-28 23:32:56 Error: Array
(
    [id] => 4
    [name] => test award
    [organisation] => test org
    [url] => http://www.example.com
    [detail] => 
    [levels] => Array
        (
            [0] => Array
                (
                    [name] => test 1
                    [id] => 4
                )

            [11] => Array
                (
                    [name] => test
                    [id] => 16
                )

        )

    [image] => Array
        (
            [name] => 
            [type] => 
            [tmp_name] => 
            [error] => 4
            [size] => 0
        )

)

【问题讨论】:

  • 而“with no lucky”翻译成什么样的具体问题?数据没有被打补丁?它被修补但没有被保存?保存失败并且实体被错误修饰? ...?
  • 它不保存关卡数据。其他奖励数据保存。它不会给出错误或在调试日志中记录任何内容

标签: cakephp-3.0 has-many


【解决方案1】:

在 LevelEntity 中验证 'id' 是否可访问

protected $_accessible = [
    '*' => true,
    'id' => true,
];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-03
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多