DataMapper ORM


Delete All

Delete All is used to delete all objects in an objects all list. It is basically quicker than looping through the all list yourself to delete each one. For example:

// Get a number of books from the year 2000
$b = new Book();
$b->where('year', 2000)->get();

// Loop through the all list and delete them one by one
foreach ($b->all as $book)
{
    $book->delete();
}

Instead just do this:

$b = new Book();
$b->where('year', 2000)->get();
$b->delete_all();

This is especially useful for deleting related items.

 

Truncate

Since Delete All will iterate over all objects in an objects all list, it will run a delete query for every record found. While this is very useful if you want to delete a subset of records in the table, it is very ineffecient when you simply want to delete all records.

If that is the goal, just use:

$b = new Book();
$b->truncate();

This will delete all records in the books table, reset all in-table-foreign-keys in related tables linking to the books table, and delete all records in relationship tables referenced by the Book model.

Note that if your relationship tables are used in multiple many-to-many relations, you should not use this method, as it will also delete all relationships between the other models using the same relationship table!