如何根据CakePHP中的条件删除子记录?(How to delete a sub record based on criteria in Cakephp?)

编程入门 行业动态 更新时间:2024-10-27 09:47:03
如何根据CakePHP中的条件删除子记录?(How to delete a sub record based on criteria in Cakephp?)

我有两个表格“Topic”和“Post”由hasMany链接到模型中

我想删除id = 3的Post条目,因为它没有消息。

在表格“Post”的模型中,我有这个“beforeSave”:

public function beforeSave($options = array()) { if ($this->data['Post']['message'] == '') { CODE TO DELETE HERE } }

这是我的$ this-> request-> data:

Array ( [Topic] => Array ( [id] => 1 [topic_title] => This is my topic ) [Post] => Array ( [1] => Array ( [id] => 1 [title] => Blah [message] => My message ) [2] => Array ( [id] => 2 [title] => Second Blah [message] => Second My message ) [3] => Array ( [id] => 3 [title] => Second Blah [message] => ) ) )

我无法弄清楚如何在模型中删除或者这是错误的方法?

我现在也在saveAssociated之前在控制器中尝试过这个:

$this->loadmodel('Post'); foreach ($this->request->data['Post'] as $i => $post) { if ($post['message'] == '') { unset($this->request->data['Post'][$i]); $options = array( 'conditions' => array( 'Post.id' => $i) ); $this->Post->find('first', $options); if ($this->Post->delete()) { echo "Post id : " . $i . ' - Deleted'; } else { echo "Post id : " . $i . ' - Not deleted'; } }

}

这给了我“Post id xxx Deleted”,但是Post记录没有被删除。

I have 2 tables "Topic" and "Post" linked in the model by hasMany

I want to delete the Post entry with id = 3 since it doesn't have a message.

In the model for table "Post", I have this "beforeSave" :

public function beforeSave($options = array()) { if ($this->data['Post']['message'] == '') { CODE TO DELETE HERE } }

This is my $this->request->data :

Array ( [Topic] => Array ( [id] => 1 [topic_title] => This is my topic ) [Post] => Array ( [1] => Array ( [id] => 1 [title] => Blah [message] => My message ) [2] => Array ( [id] => 2 [title] => Second Blah [message] => Second My message ) [3] => Array ( [id] => 3 [title] => Second Blah [message] => ) ) )

I can't figure out how to delete in the model or is this the wrong approach ?

I've now also tried this in the controller before saveAssociated :

$this->loadmodel('Post'); foreach ($this->request->data['Post'] as $i => $post) { if ($post['message'] == '') { unset($this->request->data['Post'][$i]); $options = array( 'conditions' => array( 'Post.id' => $i) ); $this->Post->find('first', $options); if ($this->Post->delete()) { echo "Post id : " . $i . ' - Deleted'; } else { echo "Post id : " . $i . ' - Not deleted'; } }

}

This gives me "Post id xxx Deleted" however the Post record isn't deleted.

最满意答案

如果消息像这样是空的,你可以在beforeSave取消设置数据:

public function beforeSave($options = array()) { foreach ($this->data['Post'] as $i => $post) { if ($post['message'] == '') { unset($this-data['Post'][$i]); } } }

Since I'm using "saveAssociated" I also have to "unset" the post in "$this->request->data" otherwise it will be saved again.

$this->loadmodel('Post'); foreach ($this->request->data['Post'] as $i => $post) { if ($post['message'] == '') { unset($this->request->data['Post'][$i]); if ($this->Post->delete($post['id'], false)) { echo "Post id : " . $i . ' - Deleted'; } else { echo "Post id : " . $i . ' - Not deleted'; } }

更多推荐

本文发布于:2023-07-22 19:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222808.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:条件   delete   CakePHP   record   Cakephp

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!