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.
- Form: delete{Record}
- Parameter: PorteRecord || int
- Return: PorteRecord The current record
Deleting a multiple records
- Form: delete{Records}
- Parameter: array || null Array containing records or primary keys or null the remove all the associations
- Return: PorteRecord The current record
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