How to delete one record with a condition in cakephp?

I am using cakephp2. How to delete one record with a condition?

I have a table with names with fields id, title, post. The primary key is id. I would like to delete one entry with id = 5?

How is this possible?

That is, I want to convert the request,

$this->query('delete * from posts where id = 5'); To cakephp ?

How is this function written in cakephp modeln named Post?

+5
source share
3 answers

You can do it like this. $this->Model->delete(5); or you can first assign the model identifier and then delete it. For instance,

$this->Model->id = 5;
$this->Model->delete();

If you want to execute a delete request (or any other) without a model, try

$db = ConnectionManager::getDataSource('default');
$db->rawQuery("DELETE FROM table WHERE id=5");
+13
source

Use deleteAll().

$this->Model->deleteAll(array('Model.field_name'=>'field_value'));

OR delete using the primary key:

1. $this->Model->delete(primary_key_value);

2. $this->Model->id = primary_key_value;
   $this->Model->delete();

Hope this helps someone.

+5
delete(int $id = null, boolean $cascade = true);

, $id. , , .

, ,

$this->Post->delete($this->request->data('Post.id'));

$this->Post->delete(5);

please let me know if I can help you more

+1
source

All Articles