1
0
mirror of synced 2024-12-13 06:46:03 +03:00

Added support for unlink all

This commit is contained in:
jackbravo 2007-09-12 22:07:57 +00:00
parent c8578d434a
commit 404338f46f
2 changed files with 19 additions and 5 deletions

View File

@ -1456,12 +1456,13 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
/**
* removeLinks
* removes links from this record to given records
* if no ids are given, it removes all links
*
* @param string $alias related component alias
* @param array $ids the identifiers of the related records
* @return Doctrine_Record this object
*/
public function unlink($alias, $ids)
public function unlink($alias, $ids = array())
{
$ids = (array) $ids;
@ -1472,8 +1473,11 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
if ($rel instanceof Doctrine_Relation_Association) {
$q->delete()
->from($rel->getAssociationTable()->getComponentName())
->where($rel->getLocal() . ' = ?', array_values($this->identifier()))
->whereIn($rel->getForeign(), $ids);
->where($rel->getLocal() . ' = ?', array_values($this->identifier()));
if (count($ids) > 0) {
$q->whereIn($rel->getForeign(), $ids);
}
$q->execute();
@ -1481,8 +1485,11 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count
} elseif ($rel instanceof Doctrine_Relation_ForeignKey) {
$q->update($rel->getTable()->getComponentName())
->set($rel->getForeign(), '?', array(null))
->addWhere($rel->getForeign() . ' = ?', array_values($this->identifier()))
->whereIn($rel->getTable()->getIdentifier(), $ids);
->addWhere($rel->getForeign() . ' = ?', array_values($this->identifier()));
if (count($ids) > 0) {
$q->whereIn($rel->getTable()->getIdentifier(), $ids);
}
$q->execute();
}

View File

@ -793,6 +793,13 @@ class Doctrine_Record_TestCase extends Doctrine_UnitTestCase
$this->assertEqual($user->Group[0]->identifier(), $group1->identifier());
$this->assertEqual($user->Group[1]->identifier(), $group2->identifier());
$user->unlink('Group');
$user->save();
unset($user);
$user = $this->objTable->find(5);
$this->assertEqual($user->Group->count(), 0);
// ACCESSING ASSOCIATION OBJECT PROPERTIES