From 404338f46fa0e857b11bd3ddc1262859523bc7bd Mon Sep 17 00:00:00 2001 From: jackbravo Date: Wed, 12 Sep 2007 22:07:57 +0000 Subject: [PATCH] Added support for unlink all --- lib/Doctrine/Record.php | 17 ++++++++++++----- tests/RecordTestCase.php | 7 +++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index 77e0bb7d1..a4e30f3e3 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -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(); } diff --git a/tests/RecordTestCase.php b/tests/RecordTestCase.php index 3ac93fc3b..ac0b05a37 100644 --- a/tests/RecordTestCase.php +++ b/tests/RecordTestCase.php @@ -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