CakePHP 3 不保存关联?
Posted
技术标签:
【中文标题】CakePHP 3 不保存关联?【英文标题】:CakePHP 3 Not Saving Associations? 【发布时间】:2015-07-31 11:51:10 【问题描述】:好的,所以我会给我以下$this->request->data
输出,
'editions' => [
'title' => 'edtionTest',
'active' => '0',
'timestamp' => (int) 1438342639
'articles' => [
(int) 0 => [
'picinfo_id' => '1',
'image' => '/img/test222.jpeg',
'title' => 'test222',
'maintxt' => 'Donec diam tortor, ullamcorper non,....'
],
(int) 1 => [
'picinfo_id' => '2',
'image' => '/img/TEST-NEW.jpeg',
'title' => 'TESTING_AGAIN',
'maintxt' => 'Donec diam tortor, ullamcorper non......'
]
],
]
然后我尝试获取表格并保存,
$GetTable = TableRegistry::get('Editions');
$NewED = $GetTable->newEntities($this->request->data(),['associated' => ['Articles']]);
debug( $GetTable->save($NewED) );
而我的调试,返回以下错误,
Argument 1 passed to Cake\ORM\Table::save() must implement interface Cake\Datasource\EntityInterface, array given,
我认为这与我的模型无关。据我所知,我已按照文件进行操作,但不确定我做错了什么?
编辑
如果我这样做,
$NewED = $GetTable->newEntity($this->request->data);
我得到一个返回错误,但是当我这样做时,
$NewEd = $GetTable->newEntity($this->request->data['editions']);
这很好,它可以保存,但它似乎只保存版本数据,与相关文章无关。
欢迎所有帮助。谢谢。
【问题讨论】:
【参考方案1】:好的,不确定这是否是最好的方法,但我再次浏览了文档并重新阅读了所有内容。
只有在循环输入时才有效,
$NewEds = $GetTable->newEntities($this->request->data());
foreach ($NewEds as $NewKey => $NewValues)
$GetTable->save($NewValues);
如果您完成了“加入”表,似乎您不必拥有associated
【讨论】:
以上是关于CakePHP 3 不保存关联?的主要内容,如果未能解决你的问题,请参考以下文章