CakePHP : Delete multiple records

Thu, Sep 20, 2007

CakePHP, PHP

With CakePHP, you can remove multiple records from a database table using the deleteAll() model method.

deleteAll() Usage

$this -> Model -> deleteAll($conditions, $cascade = true);

Lets say that you wanted to remove all the records for a specific user from the Item model. Below is a quick example.

deleteAll() Example

$this -> Item -> deleteAll(array(‘Item.user_id’ => 13));

…where 13 would be the ID of the specific user.

Off course…you can completely truncate a table with the same method, this time removing all conditions. Below is an example of how to truncate a database table using CakePHP.

deleteAll() table truncate

$this -> Model -> deleteAll();
, , , , ,
468x60gif

This post was written by:

Antonie Potgieter - who has written 57 posts on Lost-In-Code.

I (Antonie Potgieter) am a software engineer/web developer located in South Africa. My full-time work is the management of Tribulant Software and the development of its software packages.

Contact the author

2 Comments For This Post

  1. Peter Minne Says:

    The “deleteAll” requires a condition as first parameter. So your example to truncate a table should be something like this:

    $this->Model->deleteAll(’1 = 1′, false);

    Personally I think it would make sense if the cake team would implement an additional “truncate” method.

  2. jets Says:

    hey, nice tipics buddy… i used this & working great.

Leave a Reply