From 7b84d15530726f3a577dabbcbbc53584ed5e2ffa Mon Sep 17 00:00:00 2001
From: zYne <zYne@625475ce-881a-0410-a577-b389adb331d8>
Date: Wed, 27 Sep 2006 21:21:33 +0000
Subject: [PATCH] Fixes #126, #127 Ticket: 126

---
 lib/Doctrine/Collection.php       | 16 +++---
 lib/Doctrine/Collection/Batch.php | 21 +++++++-
 lib/Doctrine/Hydrate.php          |  3 +-
 lib/Doctrine/Record.php           | 83 ++++++++-----------------------
 tests/run.php                     |  1 +
 5 files changed, 53 insertions(+), 71 deletions(-)

diff --git a/lib/Doctrine/Collection.php b/lib/Doctrine/Collection.php
index bce015796..22a13757e 100644
--- a/lib/Doctrine/Collection.php
+++ b/lib/Doctrine/Collection.php
@@ -252,9 +252,9 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
 
             foreach($this->getNormalIterator() as $record) {
                 if($value !== null) {
-                    $record->rawSet($this->reference_field, $value);
+                    $record->set($this->reference_field, $value, false);
                 } else {
-                    $record->rawSet($this->reference_field, $this->reference);
+                    $record->set($this->reference_field, $this->reference, false);
                 }
             }
         } elseif($relation instanceof Doctrine_Association) {
@@ -360,7 +360,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
         if( ! isset($offset)) {
             foreach($coll as $record) {
                 if(isset($this->reference_field))
-                    $record->rawSet($this->reference_field,$this->reference);
+                    $record->set($this->reference_field,$this->reference, false);
 
                 $this->reference->addReference($record, $this->relation);
             }
@@ -431,10 +431,10 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
                 $value = $this->reference->get($this->relation->getLocal());
 
                 if($value !== null) {
-                    $this->data[$key]->rawSet($this->reference_field, $value);
+                    $this->data[$key]->set($this->reference_field, $value, false);
                 } else {
 
-                    $this->data[$key]->rawSet($this->reference_field, $this->reference);
+                    $this->data[$key]->set($this->reference_field, $this->reference, false);
                 }
             }
         }
@@ -483,7 +483,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
      */
     public function set($key,Doctrine_Record $record) {
         if(isset($this->reference_field))
-            $record->rawSet($this->reference_field,$this->reference);
+            $record->set($this->reference_field, $this->reference, false);
 
         $this->data[$key] = $record;
     }
@@ -498,7 +498,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
             return false;
         
         if(isset($this->reference_field))
-            $record->internalSet($this->reference_field,$this->reference);
+            $record->set($this->reference_field, $this->reference, false);
 
         if(isset($key)) {
             if(isset($this->data[$key]))
@@ -527,7 +527,7 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator
      */
     public function add(Doctrine_Record $record,$key = null) {
         if(isset($this->reference_field))
-            $record->rawSet($this->reference_field,$this->reference);
+            $record->set($this->reference_field, $this->reference, false);
 
         if(in_array($record,$this->data)) {
             return false;
diff --git a/lib/Doctrine/Collection/Batch.php b/lib/Doctrine/Collection/Batch.php
index 42ce5156e..e2fe1f6ab 100644
--- a/lib/Doctrine/Collection/Batch.php
+++ b/lib/Doctrine/Collection/Batch.php
@@ -1,4 +1,23 @@
 <?php
+/*
+ *  $Id$
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL. For more information, see
+ * <http://www.phpdoctrine.com>.
+ */
 Doctrine::autoload('Doctrine_Collection');
 /**
  * Doctrine_Collection_Batch       a collection of records,
@@ -148,7 +167,7 @@ class Doctrine_Collection_Batch extends Doctrine_Collection {
 
 
         if(isset($this->reference_field))
-            $this->data[$key]->rawSet($this->reference_field,$this->reference);
+            $this->data[$key]->set($this->reference_field, $this->reference, false);
 
 
         return $this->data[$key];
diff --git a/lib/Doctrine/Hydrate.php b/lib/Doctrine/Hydrate.php
index 59c42f032..280b6d353 100644
--- a/lib/Doctrine/Hydrate.php
+++ b/lib/Doctrine/Hydrate.php
@@ -414,7 +414,8 @@ abstract class Doctrine_Hydrate extends Doctrine_Access {
                                     case Doctrine_Relation::ONE_AGGREGATE:
 
                                         // one-to-one relation
-                                        $last->rawSet($fk->getLocal(), $record->getIncremented());    
+                                        if($fk instanceof Doctrine_LocalKey)
+                                            $last->set($fk->getLocal(), $record->getIncremented(), false);
 
                                         $last->initSingleReference($record, $fk);
 
diff --git a/lib/Doctrine/Record.php b/lib/Doctrine/Record.php
index 9b5de001f..55158a7d5 100644
--- a/lib/Doctrine/Record.php
+++ b/lib/Doctrine/Record.php
@@ -682,72 +682,22 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
 
         return $this->references[$name];
     }
-    /**
-     * internalSet
-     *
-     * @param mixed $name
-     * @param mixed $value
-     */
-    final public function internalSet($name, $value) {
-        if($value === null)
-            $value = self::$null;
-
-        $this->data[$name] = $value;
-    }
-    /**
-     * rawSet
-     * doctrine uses this function internally, not recommended for developers
-     *
-     * rawSet() works in very same same way as set() with an exception that
-     * 1. it cannot be used for setting references
-     * 2. it cannot load uninitialized fields
-     *
-     * @param mixed $name               name of the property or reference
-     * @param mixed $value              value of the property or reference
-     */
-    final public function rawSet($name,$value) {
-        $name = strtolower($name);
-
-        if($value instanceof Doctrine_Record)
-            $id = $value->getIncremented();
-
-        if(isset($id))
-            $value = $id;
-
-        if(isset($this->data[$name])) {
-            if($this->data[$name] === self::$null) {
-                if($this->data[$name] !== $value) {
-                    switch($this->state):
-                        case Doctrine_Record::STATE_CLEAN:
-                            $this->state = Doctrine_Record::STATE_DIRTY;
-                        break;
-                        case Doctrine_Record::STATE_TCLEAN:
-                            $this->state = Doctrine_Record::STATE_TDIRTY;
-                    endswitch;
-                }
-            }
-
-            if($this->state == Doctrine_Record::STATE_TCLEAN)
-                $this->state = Doctrine_Record::STATE_TDIRTY;
-
-            if($value === null)
-                $value = self::$null;
-
-            $this->data[$name] = $value;
-            $this->modified[]  = $name;
-        }
-    }
 
     /**
      * set
      * method for altering properties and Doctrine_Record references
+     * if the load parameter is set to false this method will not try to load uninitialized record data
      *
      * @param mixed $name                   name of the property or reference
      * @param mixed $value                  value of the property or reference
+     * @param boolean $load                 whether or not to refresh / load the uninitialized record data
+     *
      * @throws Doctrine_Record_Exception    if trying to set a value for unknown property / related component
+     * @throws Doctrine_Record_Exception    if trying to set a value of wrong type for related component
+     *
      * @return Doctrine_Record
      */
-    public function set($name,$value) {
+    public function set($name, $value, $load = true) {
         $lower = strtolower($name);
 
         if(isset($this->data[$lower])) {
@@ -759,7 +709,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
                     $value = $id;
             }
 
-            $old = $this->get($lower, false);
+            if($load)
+                $old = $this->get($lower, false);
+            else
+                $old = $this->data[$lower];
 
             if($old !== $value) {
                 
@@ -773,7 +726,6 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
                 $this->modified[]   = $lower;
                 switch($this->state):
                     case Doctrine_Record::STATE_CLEAN:
-                    case Doctrine_Record::STATE_PROXY:
                         $this->state = Doctrine_Record::STATE_DIRTY;
                     break;
                     case Doctrine_Record::STATE_TCLEAN:
@@ -947,7 +899,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
         return $a;
     }
     /**
+     * count
      * this class implements countable interface
+     *
      * @return integer                      the number of columns
      */
     public function count() {
@@ -955,13 +909,15 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
     }
     /**
      * alias for count()
+     * 
+     * @return integer
      */
     public function getColumnCount() {
         return $this->count();
     }
     /**
      * toArray
-     * returns record as an array
+     * returns the record as an array
      * 
      * @return array
      */
@@ -978,7 +934,9 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
         return $a;
     }
     /**
-     * checks if record has data
+     * exists
+     * returns true if this record is persistent, otherwise false
+     *
      * @return boolean
      */
     public function exists() {
@@ -1475,13 +1433,16 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite
      * merges this record with an array of values
      *
      * @param array $values
+     * @return void
      */
     public function merge(array $values) {
         foreach($this->table->getColumnNames() as $value) {
             try {
                 if(isset($values[$value]))
                     $this->set($value, $values[$value]);
-            } catch(Exception $e) { }
+            } catch(Exception $e) { 
+                // silence all exceptions
+            }
         }
     }
     /**
diff --git a/tests/run.php b/tests/run.php
index 08b33a8e8..af36f0c6d 100644
--- a/tests/run.php
+++ b/tests/run.php
@@ -95,6 +95,7 @@ $test->addTestCase(new Doctrine_EventListener_Chain_TestCase());
 $test->addTestCase(new Doctrine_DataDict_Sqlite_TestCase());
 
 $test->addTestCase(new Doctrine_BooleanTestCase());
+
 //$test->addTestCase(new Doctrine_Cache_FileTestCase());
 //$test->addTestCase(new Doctrine_Cache_SqliteTestCase());