From 41263d6f1c163db94f2219f61595e7a56527fe14 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Fri, 16 Aug 2013 19:27:06 +0300 Subject: [PATCH] refactor relation creation --- ipf/orm/configurable.php | 31 -------- ipf/orm/relation/parser.php | 154 ++++++++++++++++-------------------- ipf/orm/table.php | 4 +- 3 files changed, 71 insertions(+), 118 deletions(-) diff --git a/ipf/orm/configurable.php b/ipf/orm/configurable.php index a2d82bc..a19ad8f 100644 --- a/ipf/orm/configurable.php +++ b/ipf/orm/configurable.php @@ -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)) { diff --git a/ipf/orm/relation/parser.php b/ipf/orm/relation/parser.php index 1ba5da6..ce1fe89 100644 --- a/ipf/orm/relation/parser.php +++ b/ipf/orm/relation/parser.php @@ -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())); diff --git a/ipf/orm/table.php b/ipf/orm/table.php index f09b9e5..3392628 100644 --- a/ipf/orm/table.php +++ b/ipf/orm/table.php @@ -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() -- 2.49.0