Deleting associated records

Introduction

Associations will be directly deleted without the need to call the "save" method. We do not yet support cascading removal.

Currently, neither method with "has_many" of type transient, note sure wether this should ever be implemented or not.

Deleting a single record

Singular versions of delete take a primary key value or an object as parameter.

Deleting a multiple records

Plural version of delete may take a list of primary key values, and array of primary key values, an array of objects or no parameter.

If no parameter is given, all the associations of the current records are removed.

Exemple

Command: "php samples/properties/delete/exemple.php"


$groups = array();
for($i=0;$i<6;$i++){
  $group = new Group();
  $group->save();
  $groups[] = $group;
}

$user = new User();
$user->addGroups($groups);
$user->save();

// delete relation to a group based on its object
$user->deleteGroup($groups[0]);
// delete relation to a group base on its primary key
$user->deleteGroup($groups[1]->getPrimaryKey());
echo 'There are now '.$user->countGroups().' groups'."\r\n";

// delete relations to all groups
$user->deleteGroups();
echo 'There are now '.$user->countGroups().' group'."\r\n";

// There are now 4 groups
// There are now 0 group
    

Open Source Object Relational Mapping in PHP

Download Porte 0.2.1