0
Cake完全不熟悉。CakePHP:hasOne相关表在更新时保存多个条目
我有这个名为内容的表和另一个名为ContentMeta(通过content_id相关)。内容中的每个条目都有相应的ContentMeta。
当我添加新的内容,这是我用一个新的条目添加到ContentMeta代码:
if(!empty($this->data)) {
// Save Content
$this->Content->create();
$content = $this->Content->save($this->data);
// Save Meta
if(!empty($content)) {
$this->data['ContentMeta']['content_id'] = $this->Content->id;
$this->Content->ContentMeta->save($this->data);
$this->Session->setFlash('Content has been saved.');
$this->redirect(array('action' => 'edit', $this->Content->id));
}
}
这工作得很好,并添加一个新条目的内容和一个ContentMeta。
当谈到更新数据(编辑模式)时,我遇到了一个小问题。内容条目的更新顺利完成,但是在ContentMeta表中,每次更新都会创建一个新条目。
下面是更新的代码:
$this->Content->id = $id;
// Update Content
$content = $this->Content->save($this->data);
// Update Meta
if(!empty($content)) {
//Debugger::dump(print_r($this->data,true));
$this->data['ContentMeta']['content_id'] = $this->Content->id;
$this->Content->ContentMeta->save($this->data);
$this->Session->setFlash('Content has been updated.');
$this->redirect(array('action' => 'edit', $this->Content->id));
}
我敢肯定,这是在这个块中的一些逻辑上的错误是导致这种情况发生。任何人都可以给我一个想法,我错了吗?
感谢, 平方公尺Ë
完美。这样做的工作和saveAll帮助减少了大量的代码。我早些时候遇到过saveAll,但是直到我手动对各个步骤进行了试验之后,才避免使用它。谢谢尼克:) – 2010-11-19 02:35:52
所有saveAll不会调用save()几次(以及__save()),但它清理代码 – dogmatic69 2010-11-21 23:46:38