Cakephp 如何根据条件删除子记录?
How to delete a sub record based on criteria in Cakephp?
我在模型中通过 hasMany
链接了 2 tables "Topic" 和 "Post"
我想删除 ID = 3 的 Post 条目,因为它没有消息。
在 table "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 记录没有被删除。
如果消息为空,您可以取消设置 beforeSave
中的数据,如下所示:
public function beforeSave($options = array()) {
foreach ($this->data['Post'] as $i => $post) {
if ($post['message'] == '') {
unset($this-data['Post'][$i]);
}
}
}
由于我正在使用 "saveAssociated" 我还必须 "unset" "$this->request->data" 中的 post 否则它会被再次保存。
$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';
}
}
我在模型中通过 hasMany
链接了 2 tables "Topic" 和 "Post"我想删除 ID = 3 的 Post 条目,因为它没有消息。
在 table "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 记录没有被删除。
如果消息为空,您可以取消设置 beforeSave
中的数据,如下所示:
public function beforeSave($options = array()) {
foreach ($this->data['Post'] as $i => $post) {
if ($post['message'] == '') {
unset($this-data['Post'][$i]);
}
}
}
由于我正在使用 "saveAssociated" 我还必须 "unset" "$this->request->data" 中的 post 否则它会被再次保存。
$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';
}
}