How to delete a single record with where condition in cakephp?

Vinod VT picture Vinod VT · Apr 29, 2013 · Viewed 29.9k times · Source

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 ?

Answer

Preetam picture Preetam · Apr 29, 2013

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");