CakePHP:hasOne 相关表在更新时保存多个条目

发布于 2024-10-02 17:59:43 字数 1402 浏览 1 评论 0原文

蛋糕完全陌生。

我有一个名为 Content 的表和另一个名为 ContentMeta 的表(通过 content_id 相关)。 Content 中的每个条目都有一个对应的 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 ) );
 }
}

这工作正常,向 Content 添加一个新条目,向 ContentMeta 添加一个新条目。

当涉及到更新数据(编辑模式)时,我遇到了一个小问题。 Content 的条目正在顺利更新,但在 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 ) );
}

我确信这是该块中的一些逻辑错误导致了这种情况的发生。谁能告诉我我哪里出错了?

谢谢, 米^e

Totally new to Cake.

I have this table named Content and another one named ContentMeta (related via content_id). Each entry in Content has a corresponding ContentMeta.

When I add new content, this is the code I'm using to add a new entry to 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 ) );
 }
}

This works fine and add one new entry to Content and one to ContentMeta.

When it comes to updating the data (edit mode), I'm running into a slight problem. The entry for Content is getting updated without a hitch, but in the ContentMeta table, a new entry is being created upon every single update.

Here's the code for updating:

$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 ) );
}

I'm sure it's some logical error in this block that is causing this to happen. Can anyone give me an idea where I am going wrong?

Thanks,
m^e

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书间行客 2024-10-09 17:59:43

对我来说,数据数组中缺少 $this->data['ContentMeta']['id'] 。因为该字段为空,所以蛋糕尝试创建一个新行。

因此,在您的表单中为 ContentMeta 添加一个隐藏的 id 字段。

我认为可以改进代码的另一件事是使用

$this->Content->saveAll($this->data);

这种方式可以立即保存数据,而不是为每个模型调用 2 次 save() 。查看 cookbook 中的手册(并在文本中搜索 saveAll) 。

For me it's missing $this->data['ContentMeta']['id'] in your data array. And because this field is empty the cake attempt to create a new row.

So in your form add a hidden id field for the ContentMeta.

Another thing which I think will improve your code is to use

$this->Content->saveAll($this->data);

this way you will save your data at once instead of 2 save() calls for each model. Check the manual in the cookboook (and search for saveAll in the text).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文