我正在将一个正在运行的应用程序从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]); ?>
CONTROLLER
$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
我尝试了一些大写字母的变化&amp;复数。奖励数据可以正确保存,但关联的级别数据无法保存。
我缺少什么来保存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
)
)
答案 0 :(得分:1)
在LevelEntity中验证是否可以访问“id”
protected $_accessible = [
'*' => true,
'id' => true,
];