CakePHP 3.0保存belongsToMany没有关联的ID

问题描述 投票:0回答:1

我有人属于多个手机。我可以将一个人用手机保存在这样的一个保存()中吗?

在PeopleTable中:

$this->belongsToMany('Phones', [
    'foreignKey' => 'person_id',
    'targetForeignKey' => 'phone_id',
    'joinTable' => 'people_phones'
]);

在add.ctp中:

$this->Form->create($person);
$this->Form->input('person_name');
$this->Form->input('phones.0.phone_number');

在PeopleController中:

$person = $this->People->patchEntity($person, $this->request->data, ['associated' => ['Phones']]);
$this->People->save($person, ['associated' => ['Phones']])

但是没有电话,只有这个人得救了。没有错误消息。甚至可以像这样保存它吗?

我有CakePHP 3.0.2(今天更新)

Mabye我应该补充一点,我是一个初学者,以前从未用OOP编码,而且我现在折磨谷歌两天没有任何相关的答案。简单地说“是的,它应该以这种方式工作,你必须有一些错误”或“不,你必须首先保存电话才能获得它的ID”就足够了:)谢谢

has-and-belongs-to-many cakephp-3.0
1个回答
0
投票

感谢@ndm,在$ _accessible属性的Person实体类中发现了问题。它应该如下所示:

protected $_accessible = [
    'person_name' => true,
    'phones' => true,
];

现在它完美无缺!非常感谢你

© www.soinside.com 2019 - 2024. All rights reserved.