I am using cakephp2. How to delete a single record with a condition ?
I have a table named posts with fields id,title,post. primary key is id. I would like to delete the single record with id=5 ?
How it possible ?
That is I wands to convert the query,
$this->query('delete * from posts where id = 5'); To cakephp ?
How this function is write on cakephp modeln named Post ?
You can do it like this. $this->Model->delete(5);
or you can assign id to the model first and then delete it. Such as
$this->Model->id = 5;
$this->Model->delete();
If you want to execute a delete (or any other) query without a model then you should try
$db = ConnectionManager::getDataSource('default');
$db->rawQuery("DELETE FROM table WHERE id=5");
Use deleteAll()
.
$this->Model->deleteAll(array('Model.field_name'=>'field_value'));
OR delete by primary key:
1. $this->Model->delete(primary_key_value);
2. $this->Model->id = primary_key_value;
$this->Model->delete();
Hope it will help someone.
delete(int $id = null, boolean $cascade = true);
Deletes the record identified by $id. By default, also deletes records dependent on the record specified to be deleted.
Also you can apply function like in controller to delete one record
$this->Post->delete($this->request->data('Post.id'));
or if static id is known to you
$this->Post->delete(5);
please let me know if i can help you more
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With