diff --git a/lib/Doctrine/Collection.php b/lib/Doctrine/Collection.php index 511c7ce70..5aadbc0d7 100644 --- a/lib/Doctrine/Collection.php +++ b/lib/Doctrine/Collection.php @@ -912,6 +912,24 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator return $this; } + + public function free($deep = false) + { + foreach ($this->getData() as $key => $record) { + if ( ! ($record instanceof Doctrine_Null)) { + $record->free($deep); + } + } + + $this->data = array(); + + if ($this->reference) { + $this->reference->free($deep); + $this->reference = null; + } + } + + /** * getIterator * @return object ArrayIterator diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php index 3cc4ce5ee..ddad411b6 100644 --- a/lib/Doctrine/Record.php +++ b/lib/Doctrine/Record.php @@ -1902,11 +1902,22 @@ abstract class Doctrine_Record extends Doctrine_Record_Abstract implements Count */ public function free() { - $this->_mapper->detach($this); - $this->_mapper->removeRecord($this); - $this->_data = array(); - $this->_id = array(); - $this->_references = array(); + if ($this->_state != self::STATE_LOCKED) { + $this->_mapper->detach($this); + $this->_mapper->removeRecord($this); + $this->_data = array(); + $this->_id = array(); + + if ($deep) { + foreach ($this->_references as $name => $reference) { + if ( ! ($reference instanceof Doctrine_Null)) { + $reference->free($deep); + } + } + } + + $this->_references = array(); + } } - + }