]> git.andy128k.dev Git - ipf-legacy-orm.git/commitdiff
refactor relation creation
authorAndrey Kutejko <andy128k@gmail.com>
Fri, 16 Aug 2013 16:27:06 +0000 (19:27 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Fri, 16 Aug 2013 16:27:06 +0000 (19:27 +0300)
ipf/orm/configurable.php
ipf/orm/relation/parser.php
ipf/orm/table.php

index a2d82bc8f3011c0ca88e3db524bebb571d14b4ed..a19ad8ff959215a942e9518b01963922f935b9be 100644 (file)
@@ -4,7 +4,6 @@ abstract class IPF_ORM_Configurable
 {
     protected $attributes = array();
     protected $parent;
-    protected $_impl = array();
     protected $_params = array();
 
     public function getAttributeFromString($stringAttributeName)
@@ -133,36 +132,6 @@ abstract class IPF_ORM_Configurable
         return $this->_params[$namespace][$name];
     }
 
-    public function setImpl($template, $class)
-    {
-        $this->_impl[$template] = $class;
-
-        return $this;
-    }
-
-    public function getImpl($template)
-    {
-        if ( ! isset($this->_impl[$template])) {
-            if (isset($this->parent)) {
-                return $this->parent->getImpl($template);
-            }
-            return null;
-        }
-        return $this->_impl[$template];
-    }
-    
-    
-    public function hasImpl($template)
-    {
-        if ( ! isset($this->_impl[$template])) {
-            if (isset($this->parent)) {
-                return $this->parent->hasImpl($template);
-            }
-            return false;
-        }
-        return true;
-    }
-
     public function getAttribute($attribute)
     {
         if (is_string($attribute)) {
index 1ba5da69eabe84d559ea30c8def1456069e0892e..ce1fe8920a568d8f0c27b9db8e7a57a65c3a6d86 100644 (file)
@@ -54,114 +54,98 @@ class IPF_ORM_Relation_Parser
         $this->_pending[$alias] = $options;
     }
 
-    public function getRelation($alias, $recursive = true)
+    private function createRelation($alias)
     {
-        if (isset($this->_relations[$alias])) {
-            return $this->_relations[$alias];
-        }
+        if (isset($this->_relations[$alias]))
+            return;
 
-        if (isset($this->_pending[$alias])) {
-            $def = $this->_pending[$alias];
-            $identifierColumnNames = $this->_table->getIdentifierColumnNames();
-            $idColumnName = array_pop($identifierColumnNames);
-
-            // check if reference class name exists
-            // if it does we are dealing with association relation
-            if (isset($def['refClass'])) {
-                $def = $this->completeAssocDefinition($def);
-                $localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName()));
-
-                if ( ! isset($this->_pending[$def['refClass']]) &&
-                     ! isset($this->_relations[$def['refClass']])) {
-
-                    $parser = $def['refTable']->getRelationParser();
-                    if ( ! $parser->hasRelation($this->_table->getComponentName())) {
-                        $parser->bind($this->_table->getComponentName(), null, IPF_ORM_Relation::ONE, array(
-                            'local'     => $def['local'],
-                            'foreign'   => $idColumnName,
-                            'localKey'  => true,
-                        ));
-                    }
+        if (!isset($this->_pending[$alias]))
+            throw new IPF_ORM_Exception('Unknown relation alias "' . $alias . '".');
 
-                    if (!$this->hasRelation($def['refClass'])) {
-                        $this->bind($def['refClass'], null, IPF_ORM_Relation::MANY, array(
-                            'foreign' => $def['local'],
-                            'local'   => $idColumnName,
-                        ));
-                    }
+        $def = $this->_pending[$alias];
+        $identifierColumnNames = $this->_table->getIdentifierColumnNames();
+        $idColumnName = array_pop($identifierColumnNames);
+
+        // check if reference class name exists
+        // if it does we are dealing with association relation
+        if (isset($def['refClass'])) {
+            $def = $this->completeAssocDefinition($def);
+            $localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName()));
+
+            if ( ! isset($this->_pending[$def['refClass']]) &&
+                 ! isset($this->_relations[$def['refClass']])) {
+
+                $parser = $def['refTable']->getRelationParser();
+                if ( ! $parser->hasRelation($this->_table->getComponentName())) {
+                    $parser->bind($this->_table->getComponentName(), null, IPF_ORM_Relation::ONE, array(
+                        'local'     => $def['local'],
+                        'foreign'   => $idColumnName,
+                        'localKey'  => true,
+                    ));
                 }
-                if (in_array($def['class'], $localClasses)) {
-                    $rel = new IPF_ORM_Relation_Nest($def);
-                } else {
-                    $rel = new IPF_ORM_Relation_Association($def);
+
+                if (!$this->hasRelation($def['refClass'])) {
+                    $this->bind($def['refClass'], null, IPF_ORM_Relation::MANY, array(
+                        'foreign' => $def['local'],
+                        'local'   => $idColumnName,
+                    ));
                 }
+            }
+            if (in_array($def['class'], $localClasses)) {
+                $rel = new IPF_ORM_Relation_Nest($def);
             } else {
-                // simple foreign key relation
-                $def = $this->completeDefinition($def);
+                $rel = new IPF_ORM_Relation_Association($def);
+            }
+        } else {
+            // simple foreign key relation
+            $def = $this->completeDefinition($def);
 
-                if (isset($def['localKey'])) {
-                    $rel = new IPF_ORM_Relation_LocalKey($def);
+            if (isset($def['localKey'])) {
+                $rel = new IPF_ORM_Relation_LocalKey($def);
 
-                    // Automatically index foreign keys which are not primary
-                    $foreign = (array) $def['foreign'];
-                    foreach ($foreign as $fk) {
-                        if ( ! $rel->getTable()->isIdentifier($fk)) {
-                            $rel->getTable()->addIndex($fk, array('fields' => array($fk)));
-                        }
+                // Automatically index foreign keys which are not primary
+                $foreign = (array) $def['foreign'];
+                foreach ($foreign as $fk) {
+                    if ( ! $rel->getTable()->isIdentifier($fk)) {
+                        $rel->getTable()->addIndex($fk, array('fields' => array($fk)));
                     }
-                } else {
-                    $rel = new IPF_ORM_Relation_ForeignKey($def);
                 }
-            }
-            if (isset($rel)) {
-                // unset pending relation
-                unset($this->_pending[$alias]);
-
-                $this->_relations[$alias] = $rel;
-                return $rel;
+            } else {
+                $rel = new IPF_ORM_Relation_ForeignKey($def);
             }
         }
-        if ($recursive) {
-            $this->getRelations();
-            return $this->getRelation($alias, false);
-        } else {
-            throw new IPF_ORM_Exception('Unknown relation alias "' . $alias . '".');
+
+        if (isset($rel)) {
+            // unset pending relation
+            unset($this->_pending[$alias]);
+
+            $this->_relations[$alias] = $rel;
         }
     }
 
-    public function getRelations()
+    public function getRelation($alias)
     {
-        foreach ($this->_pending as $k => $v) {
-            $this->getRelation($k);
-        }
-
-        return $this->_relations;
+        $this->getRelations();
+        return $this->_relations[$alias];
     }
 
-    public function getImpl($template)
+    public function getRelations()
     {
-        $conn = $this->_table->getConnection();
-
-        if (in_array('IPF_ORM_Template', class_parents($template))) {
-            $impl = $this->_table->getImpl($template);
-
-            if ($impl === null) {
-                throw new IPF_ORM_Exception("Couldn't find concrete implementation for template " . $template);
-            }
-        } else {
-            $impl = $template;
+        foreach ($this->_pending as $k => $v) {
+            $this->createRelation($k);
         }
 
-        return $conn->getTable($impl);
+        return $this->_relations;
     }
 
     public function completeAssocDefinition($def)
     {
         $conn = $this->_table->getConnection();
-        $def['table'] = $this->getImpl($def['class']);
-        $def['localTable'] = $this->_table;
-        $def['class'] = $def['table']->getComponentName();
-        $def['refTable'] = $this->getImpl($def['refClass']);
+
+        $def['table']       = IPF_ORM::getTable($def['class']);
+        $def['localTable']  = $this->_table;
+        $def['class']       = $def['table']->getComponentName();
+        $def['refTable']    = IPF_ORM::getTable($def['refClass']);
 
         $id = $def['refTable']->getIdentifierColumnNames();
 
@@ -249,9 +233,9 @@ class IPF_ORM_Relation_Parser
     public function completeDefinition($def)
     {
         $conn = $this->_table->getConnection();
-        $def['table'] = $this->getImpl($def['class']);
-        $def['localTable'] = $this->_table;
-        $def['class'] = $def['table']->getComponentName();
+        $def['table']       = IPF_ORM::getTable($def['class']);
+        $def['localTable']  = $this->_table;
+        $def['class']       = $def['table']->getComponentName();
 
         $foreignClasses = array_merge($def['table']->getOption('parents'), array($def['class']));
         $localClasses   = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName()));
index f09b9e55e0416668b200ac8d4ff93941ac35049d..3392628b6eeb76d5e41053d53af51ee57e6060b4 100644 (file)
@@ -252,9 +252,9 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable
         return $this->_parser->hasRelation($alias);
     }
 
-    public function getRelation($alias, $recursive = true)
+    public function getRelation($alias)
     {
-        return $this->_parser->getRelation($alias, $recursive);
+        return $this->_parser->getRelation($alias);
     }
 
     public function getRelations()