From 12a9f30a766ee1472f5f639e3d173e800628adca Mon Sep 17 00:00:00 2001
From: zYne <zYne@625475ce-881a-0410-a577-b389adb331d8>
Date: Wed, 30 May 2007 10:38:32 +0000
Subject: [PATCH]

---
 lib/Doctrine/Hydrate/Array.php  |  4 +++
 lib/Doctrine/Hydrate/Record.php | 49 +++++++++++++++++++++++++--------
 2 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/lib/Doctrine/Hydrate/Array.php b/lib/Doctrine/Hydrate/Array.php
index 7c7f5dbaf..e1a951771 100644
--- a/lib/Doctrine/Hydrate/Array.php
+++ b/lib/Doctrine/Hydrate/Array.php
@@ -41,6 +41,10 @@ class Doctrine_Hydrate_Array
     {
         return $data;
     }
+    public function isIdentifiable(array $data, Doctrine_Table $table)
+    {
+        return true;
+    }
     public function registerCollection($coll)
     {
 
diff --git a/lib/Doctrine/Hydrate/Record.php b/lib/Doctrine/Hydrate/Record.php
index 896b2f0e8..28db5dafb 100644
--- a/lib/Doctrine/Hydrate/Record.php
+++ b/lib/Doctrine/Hydrate/Record.php
@@ -36,6 +36,8 @@ class Doctrine_Hydrate_Record
     protected $_collections = array();
     
     protected $_records = array();
+    
+    protected $_tables = array();
 
     public function getElementCollection($component)
     {
@@ -46,19 +48,46 @@ class Doctrine_Hydrate_Record
     }
     public function registerCollection($coll)
     {
-
-    	if ( ! is_object($coll)) {
-    	   throw new Exception();
-    	}
         $this->_collections[] = $coll;
+    }
+    /**
+     * isIdentifiable
+     * returns whether or not a given data row is identifiable (it contains
+     * all primary key fields specified in the second argument)
+     *
+     * @param array $row
+     * @param Doctrine_Table $table
+     * @return boolean
+     */
+    public function isIdentifiable(array $row, Doctrine_Table $table)
+    {
+    	$primaryKeys = $table->getIdentifier();
 
+        if (is_array($primaryKeys)) {
+            foreach ($primaryKeys as $id) {
+                if ( ! isset($row[$id])) {
+                    return false;
+                }
+            }
+        } else {
+            if ( ! isset($row[$primaryKeys])) {
+                return false;
+            }
+        }
+        return true;
     }
     public function getElement(array $data, $component)
     {
-        $record = new $component();
-
-        $record->hydrate($data);
+    	if ( ! isset($this->_tables[$component])) {
+            $this->_tables[$component] = Doctrine_Manager::getInstance()->getTable($component);
+        }
+        $this->_tables[$component]->setData($data);
+        $record = $this->_tables[$component]->getRecord();
         $this->_records[] = $record;
+        
+
+        $this->_tables[$component]->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false);
+
 
         return $record;
     }
@@ -68,10 +97,8 @@ class Doctrine_Hydrate_Record
         foreach (array_unique($this->_collections) as $key => $coll) {
             $coll->takeSnapshot();
         }
-
-        foreach ($this->_records as $record) {
-            $record->state(Doctrine_Record::STATE_CLEAN);
+        foreach ($this->_tables as $table) {
+            $table->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, true);
         }
-
     }
 }
-- 
2.21.0