如何使用$item = $this->Items->newEntity();
在foreach循环中保存来自$this->request->data
的数据。
$item = $this->Items->newEntity();
foreach ($array as $item) {
//added for sake of answer
$item = $this->Items->newEntity();
$this->request->data['objects']['_ids'][] = $item['object_id'];
$this->Items->patchEntity($item, $this->request->data);
$this->Items->save($item);
//added
unset($this->request->data['object']['_ids']);
}
当newEntity()
超出foreach时,有一个item.id
和正确数量的object.ids但与同一item.id
有关。当我将newEntity()
设置为foreach循环内部时,我的item.ids
数量正确,但表object.id
中items_object
的数量不正确(超过了数组)。
答案:
将newEntity()
放在foreach中,将unset($this->request->data['object']['_ids']);
放在最后。它似乎产生了正确的条目。