DataMapper


Delete

There are a number of ways to run Delete and its effect will be different depending on whether you pass in a parameter. It's very similar to the Save function.

Note:  Delete should only be used on existing objects.

Delete on an Existing Object

Running Delete on an existing object will delete its corresponding record from the database.

Note:  When you delete an object, all its relations to other objects will also be deleted. Free house cleaning! :)

// Get user foo
$u = new User();
$u->where('username', 'foo')->get();

// Delete user
$u->delete();

Delete a Relationship on an Existing Object

It's easy to delete the relationships your objects have with each other, and there are a few ways of doing it. It's

Note:  You can only delete relations from objects that already exist in the Database.

Delete a Single Relation

To delete a relation, you pass the object you want to delete the relation to, into your current object.

// Get user foo
$u = new User();
$u->where('username', 'foo')->get();

// Get country object for Australia
$c = new Country();
$c->where('name', 'Australia')->get();

// Delete relation between user foo and country Australia
$u->delete($c);

Delete Multiple Relations

To delete multiple relations, you pass an object's all property or an array of objects.

// Get user foo
$u = new User();
$u->where('username', 'foo')->get();


// Get country object for Australia
$c = new Country();
$c->where('name', 'Australia')->get();

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

// Get a movie with ID of 5
$m = new Movie();
$m->where('id', 5)->get();

// Delete relation between user foo and all the books
$u->delete($b->all);

// Or we could pass everything in one go (it's ok to have a mix of single objects and all lists from objects)
$u->delete(array($c, $b->all, $m));