From 9f1d1ddb20bf326ac21badce03c5594769d7100a Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sat, 27 Jul 2013 22:41:06 +0300 Subject: [PATCH] refactor relation definition --- ipf/admin/models/_generated/BaseAdminLog.php | 3 +- ipf/auth/models/_generated/BasePermission.php | 18 +--- ipf/auth/models/_generated/BaseRole.php | 18 +--- .../models/_generated/BaseRolePermission.php | 10 +- ipf/auth/models/_generated/BaseUser.php | 21 ++-- .../models/_generated/BaseUserPermission.php | 10 +- ipf/auth/models/_generated/BaseUserRole.php | 10 +- ipf/orm/import/builder.php | 97 ++++++++----------- ipf/orm/record/abstract.php | 25 ----- ipf/orm/relation/parser.php | 48 ++++----- ipf/orm/table.php | 54 ++++------- ipf/session/models/_generated/BaseSession.php | 1 - 12 files changed, 102 insertions(+), 213 deletions(-) diff --git a/ipf/admin/models/_generated/BaseAdminLog.php b/ipf/admin/models/_generated/BaseAdminLog.php index b10e831..6f71fc1 100644 --- a/ipf/admin/models/_generated/BaseAdminLog.php +++ b/ipf/admin/models/_generated/BaseAdminLog.php @@ -31,7 +31,8 @@ abstract class BaseAdminLog extends IPF_ORM_Record public function setUp() { - $this->getTable()->addTemplate(new IPF_ORM_Template_Timestampable(array('updated' => array('disabled' => true)))); + $table = $this->getTable(); + $table->addTemplate(new IPF_ORM_Template_Timestampable(array('updated' => array('disabled' => true)))); } public static function table() diff --git a/ipf/auth/models/_generated/BasePermission.php b/ipf/auth/models/_generated/BasePermission.php index 50f1231..1e3db87 100644 --- a/ipf/auth/models/_generated/BasePermission.php +++ b/ipf/auth/models/_generated/BasePermission.php @@ -21,19 +21,11 @@ abstract class BasePermission extends IPF_ORM_Record public function setUp() { - $this->hasMany('User as Users', array('refClass' => 'UserPermission', - 'local' => 'permission_id', - 'foreign' => 'user_id')); - - $this->hasMany('Role as Roles', array('refClass' => 'RolePermission', - 'local' => 'permission_id', - 'foreign' => 'role_id')); - - $this->hasMany('RolePermission', array('local' => 'id', - 'foreign' => 'permission_id')); - - $this->hasMany('UserPermission', array('local' => 'id', - 'foreign' => 'permission_id')); + $table = $this->getTable(); + $table->hasMany('User', 'Users', array('refClass' => 'UserPermission', 'local' => 'permission_id', 'foreign' => 'user_id')); + $table->hasMany('Role', 'Roles', array('refClass' => 'RolePermission', 'local' => 'permission_id', 'foreign' => 'role_id')); + $table->hasMany('RolePermission', '', array('local' => 'id', 'foreign' => 'permission_id')); + $table->hasMany('UserPermission', '', array('local' => 'id', 'foreign' => 'permission_id')); } public static function table() diff --git a/ipf/auth/models/_generated/BaseRole.php b/ipf/auth/models/_generated/BaseRole.php index f600980..8b956c7 100644 --- a/ipf/auth/models/_generated/BaseRole.php +++ b/ipf/auth/models/_generated/BaseRole.php @@ -21,19 +21,11 @@ abstract class BaseRole extends IPF_ORM_Record public function setUp() { - $this->hasMany('Permission as Permissions', array('refClass' => 'RolePermission', - 'local' => 'role_id', - 'foreign' => 'permission_id')); - - $this->hasMany('User as Users', array('refClass' => 'UserRole', - 'local' => 'role_id', - 'foreign' => 'user_id')); - - $this->hasMany('RolePermission', array('local' => 'id', - 'foreign' => 'role_id')); - - $this->hasMany('UserRole', array('local' => 'id', - 'foreign' => 'role_id')); + $table = $this->getTable(); + $table->hasMany('Permission', 'Permissions', array('refClass' => 'RolePermission', 'local' => 'role_id', 'foreign' => 'permission_id')); + $table->hasMany('User', 'Users', array('refClass' => 'UserRole', 'local' => 'role_id', 'foreign' => 'user_id')); + $table->hasMany('RolePermission', '', array('local' => 'id', 'foreign' => 'role_id')); + $table->hasMany('UserRole', '', array('local' => 'id', 'foreign' => 'role_id')); } public static function table() diff --git a/ipf/auth/models/_generated/BaseRolePermission.php b/ipf/auth/models/_generated/BaseRolePermission.php index 85bc274..6f58632 100644 --- a/ipf/auth/models/_generated/BaseRolePermission.php +++ b/ipf/auth/models/_generated/BaseRolePermission.php @@ -22,13 +22,9 @@ abstract class BaseRolePermission extends IPF_ORM_Record public function setUp() { - $this->hasOne('Role', array('local' => 'role_id', - 'foreign' => 'id', - 'onDelete' => 'CASCADE')); - - $this->hasOne('Permission', array('local' => 'permission_id', - 'foreign' => 'id', - 'onDelete' => 'CASCADE')); + $table = $this->getTable(); + $table->hasOne('Role', '', array('local' => 'role_id', 'foreign' => 'id', 'onDelete' => 'CASCADE')); + $table->hasOne('Permission', '', array('local' => 'permission_id', 'foreign' => 'id', 'onDelete' => 'CASCADE')); } public static function table() diff --git a/ipf/auth/models/_generated/BaseUser.php b/ipf/auth/models/_generated/BaseUser.php index 58fefed..e6ca794 100644 --- a/ipf/auth/models/_generated/BaseUser.php +++ b/ipf/auth/models/_generated/BaseUser.php @@ -29,21 +29,12 @@ abstract class BaseUser extends IPF_ORM_Record public function setUp() { - $this->hasMany('Role as Roles', array('refClass' => 'UserRole', - 'local' => 'user_id', - 'foreign' => 'role_id')); - - $this->hasMany('Permission as Permissions', array('refClass' => 'UserPermission', - 'local' => 'user_id', - 'foreign' => 'permission_id')); - - $this->hasMany('UserRole', array('local' => 'id', - 'foreign' => 'user_id')); - - $this->hasMany('UserPermission', array('local' => 'id', - 'foreign' => 'user_id')); - - $this->getTable()->addTemplate(new IPF_ORM_Template_Timestampable()); + $table = $this->getTable(); + $table->hasMany('Role', 'Roles', array('refClass' => 'UserRole', 'local' => 'user_id', 'foreign' => 'role_id')); + $table->hasMany('Permission', 'Permissions', array('refClass' => 'UserPermission', 'local' => 'user_id', 'foreign' => 'permission_id')); + $table->hasMany('UserRole', '', array('local' => 'id', 'foreign' => 'user_id')); + $table->hasMany('UserPermission', '', array('local' => 'id', 'foreign' => 'user_id')); + $table->addTemplate(new IPF_ORM_Template_Timestampable()); } public static function table() diff --git a/ipf/auth/models/_generated/BaseUserPermission.php b/ipf/auth/models/_generated/BaseUserPermission.php index eee40b0..9c2d55f 100644 --- a/ipf/auth/models/_generated/BaseUserPermission.php +++ b/ipf/auth/models/_generated/BaseUserPermission.php @@ -22,13 +22,9 @@ abstract class BaseUserPermission extends IPF_ORM_Record public function setUp() { - $this->hasOne('User', array('local' => 'user_id', - 'foreign' => 'id', - 'onDelete' => 'CASCADE')); - - $this->hasOne('Permission', array('local' => 'permission_id', - 'foreign' => 'id', - 'onDelete' => 'CASCADE')); + $table = $this->getTable(); + $table->hasOne('User', '', array('local' => 'user_id', 'foreign' => 'id', 'onDelete' => 'CASCADE')); + $table->hasOne('Permission', '', array('local' => 'permission_id', 'foreign' => 'id', 'onDelete' => 'CASCADE')); } public static function table() diff --git a/ipf/auth/models/_generated/BaseUserRole.php b/ipf/auth/models/_generated/BaseUserRole.php index 7b5c82a..f29522e 100644 --- a/ipf/auth/models/_generated/BaseUserRole.php +++ b/ipf/auth/models/_generated/BaseUserRole.php @@ -22,13 +22,9 @@ abstract class BaseUserRole extends IPF_ORM_Record public function setUp() { - $this->hasOne('User', array('local' => 'user_id', - 'foreign' => 'id', - 'onDelete' => 'CASCADE')); - - $this->hasOne('Role', array('local' => 'role_id', - 'foreign' => 'id', - 'onDelete' => 'CASCADE')); + $table = $this->getTable(); + $table->hasOne('User', '', array('local' => 'user_id', 'foreign' => 'id', 'onDelete' => 'CASCADE')); + $table->hasOne('Role', '', array('local' => 'role_id', 'foreign' => 'id', 'onDelete' => 'CASCADE')); } public static function table() diff --git a/ipf/orm/import/builder.php b/ipf/orm/import/builder.php index f6b234f..9cf200a 100644 --- a/ipf/orm/import/builder.php +++ b/ipf/orm/import/builder.php @@ -57,22 +57,20 @@ class IPF_ORM_Import_Builder private function buildSetUp(array $definition) { $ret = array(); - $i = 0; - if (isset($definition['relations']) && is_array($definition['relations']) && ! empty($definition['relations'])) { + if (isset($definition['relations']) && is_array($definition['relations']) && !empty($definition['relations'])) { foreach ($definition['relations'] as $name => $relation) { - $class = isset($relation['class']) ? $relation['class']:$name; - $alias = (isset($relation['alias']) && $relation['alias'] !== $relation['class']) ? ' as ' . $relation['alias'] : ''; + $class = isset($relation['class']) ? $relation['class'] : $name; + $alias = (isset($relation['alias']) && $relation['alias'] !== $relation['class']) ? $relation['alias'] : ''; - if ( ! isset($relation['type'])) { + if (!isset($relation['type'])) $relation['type'] = IPF_ORM_Relation::ONE; - } if ($relation['type'] === IPF_ORM_Relation::ONE || $relation['type'] === IPF_ORM_Relation::ONE_COMPOSITE) { - $ret[$i] = " ".'$this->hasOne(\'' . $class . $alias . '\''; + $r = " \$table->hasOne('$class', '$alias'"; } else { - $ret[$i] = " ".'$this->hasMany(\'' . $class . $alias . '\''; + $r = " \$table->hasMany('$class', '$alias'"; } $a = array(); @@ -113,51 +111,44 @@ class IPF_ORM_Import_Builder $a[] = '\'exclude\' => ' . self::varExport($relation['exclude']); } - if ( ! empty($a)) { - $ret[$i] .= ', ' . 'array('; - $length = strlen($ret[$i]); - $ret[$i] .= implode(',' . PHP_EOL . str_repeat(' ', $length), $a) . ')'; - } + if (!empty($a)) + $r .= ', array(' . implode(', ', $a) . ')'; - $ret[$i] .= ');'.PHP_EOL; - $i++; + $ret[] = $r.');'; } } if (isset($definition['templates']) && is_array($definition['templates']) && !empty($definition['templates'])) { - $ret[$i] = $this->buildTemplates($definition['templates']); - $i++; + $this->buildTemplates($definition['templates'], $ret); } if (isset($definition['actAs']) && is_array($definition['actAs']) && !empty($definition['actAs'])) { - $ret[$i] = $this->buildActAs($definition['actAs']); - $i++; + $this->buildActAs($definition['actAs'], $ret); } if (isset($definition['listeners']) && is_array($definition['listeners']) && !empty($definition['listeners'])) { - foreach($definition['listeners'] as $listener) { - $ret[$i] = PHP_EOL.' $this->getTable()->listeners[\''.$listener.'\'] = new '.$listener.'();'; - $i++; + foreach ($definition['listeners'] as $listener) { + $ret[] = " \$table->listeners['$listener'] = new $listener;"; } } - $code = implode(PHP_EOL, $ret); - $code = trim($code); - - // If the body of the function has contents then we need to - if ($code) { - // If the body of the function has contents and we are using inheritance - // then we need call the parent::setUp() before the body of the function - // Class table inheritance is the only one we shouldn't call parent::setUp() for - if ($code && isset($definition['inheritance']['type']) && $definition['inheritance']['type'] != 'class_table') { - $code = "parent::setUp();" . PHP_EOL . ' ' . $code; - } + // If the body of the function has contents and we are using inheritance + // then we need call the parent::setUp() before the body of the function + // Class table inheritance is the only one we shouldn't call parent::setUp() for + if (count($ret) && isset($definition['inheritance']['type']) && $definition['inheritance']['type'] != 'class_table') { + array_unshift($ret, ' parent::setUp();'); } // If we have some code for the function then lets define it and return it - if ($code) { - return ' public function setUp()' . PHP_EOL . ' {' . PHP_EOL . ' ' . $code . PHP_EOL . ' }'; + if (count($ret)) { + array_unshift($ret, + ' public function setUp()', + ' {', + ' $table = $this->getTable();' + ); + $ret[] = ' }'; } + return $ret; } private function buildColumns(array $columns) @@ -222,28 +213,20 @@ class IPF_ORM_Import_Builder return implode(PHP_EOL, $result); } - private function buildTemplates(array $templates) + private function buildTemplates(array $templates, array &$build) { - $build = ''; foreach ($templates as $name => $options) { - if (is_array($options) && !empty($options)) { - $optionsPhp = self::varExport($options); - - $build .= " \$this->getTable()->addTemplate('" . $name . "', " . $optionsPhp . ");" . PHP_EOL; + $build[] = " \$table->addTemplate('$name', " . self::varExport($options) . ");"; + } elseif (isset($templates[0])) { + $build[] = " \$table->addTemplate('$options');"; } else { - if (isset($templates[0])) { - $build .= " \$this->getTable()->addTemplate('" . $options . "');" . PHP_EOL; - } else { - $build .= " \$this->getTable()->addTemplate('" . $name . "');" . PHP_EOL; - } + $build[] = " \$table->addTemplate('$name');"; } } - - return $build; } - private function buildActAs($actAs) + private function buildActAs($actAs, array &$build) { // rewrite special case of actAs: [Behavior] which gave [0] => Behavior if (is_array($actAs) && isset($actAs[0]) && !is_array($actAs[0])) { @@ -254,24 +237,20 @@ class IPF_ORM_Import_Builder if (!is_array($actAs)) $actAs = array($actAs => ''); - $build = ''; - foreach($actAs as $template => $options) { + foreach ($actAs as $template => $options) { // find class matching $name - if (class_exists("IPF_ORM_Template_$template", true)) { - $classname = "IPF_ORM_Template_$template"; - } else { + if (class_exists('IPF_ORM_Template_'.$template, true)) + $classname = 'IPF_ORM_Template_'.$template; + else $classname = $template; - } if (is_array($options)) $options = self::varExport($options); else $options = ''; - $build .= " \$this->getTable()->addTemplate(new $classname($options));" . PHP_EOL; + $build[] = " \$table->addTemplate(new $classname($options));"; } - - return $build; } private function buildAttributes(array $attributes) @@ -341,7 +320,7 @@ class IPF_ORM_Import_Builder $code[] = '{'; $code[] = $this->buildTableDefinition($definition); $code[] = ''; - $code[] = $this->buildSetUp($definition); + $code = array_merge($code, $this->buildSetUp($definition)); $code[] = ''; $code = array_merge($code, $this->buildShortcuts($definition)); $code[] = '}'; diff --git a/ipf/orm/record/abstract.php b/ipf/orm/record/abstract.php index 15183d6..dc9f4a7 100644 --- a/ipf/orm/record/abstract.php +++ b/ipf/orm/record/abstract.php @@ -37,31 +37,6 @@ abstract class IPF_ORM_Record_Abstract extends IPF_ORM_Access } } - public function ownsOne() - { - $this->_table->bind(func_get_args(), IPF_ORM_Relation::ONE_COMPOSITE); - - return $this; - } - - public function ownsMany() - { - $this->_table->bind(func_get_args(), IPF_ORM_Relation::MANY_COMPOSITE); - return $this; - } - - public function hasOne() - { - $this->_table->bind(func_get_args(), IPF_ORM_Relation::ONE_AGGREGATE); - return $this; - } - - public function hasMany() - { - $this->_table->bind(func_get_args(), IPF_ORM_Relation::MANY_AGGREGATE); - return $this; - } - public function bindQueryParts(array $queryParts) { $this->_table->bindQueryParts($queryParts); diff --git a/ipf/orm/relation/parser.php b/ipf/orm/relation/parser.php index a5e36d6..1ba5da6 100644 --- a/ipf/orm/relation/parser.php +++ b/ipf/orm/relation/parser.php @@ -37,31 +37,21 @@ class IPF_ORM_Relation_Parser public function hasRelation($name) { - if ( ! isset($this->_pending[$name]) && ! isset($this->_relations[$name])) { - return false; - } - - return true; + return isset($this->_pending[$name]) || isset($this->_relations[$name]); } - public function bind($name, $options = array()) + public function bind($name, $alias, $type, $options=array()) { - $e = explode(' as ', $name); - $name = $e[0]; - $alias = isset($e[1]) ? $e[1] : $name; + if (!$alias) + $alias = $name; - if ( ! isset($options['type'])) { - throw new IPF_ORM_Exception('Relation type not set.'); - } - - if ($this->hasRelation($alias)) { - unset($this->relations[$alias]); - unset($this->_pending[$alias]); - } + unset($this->relations[$alias]); - $this->_pending[$alias] = array_merge($options, array('class' => $name, 'alias' => $alias)); + $options['class'] = $name; + $options['alias'] = $alias; + $options['type'] = $type; - return $this->_pending[$alias]; + $this->_pending[$alias] = $options; } public function getRelation($alias, $recursive = true) @@ -86,18 +76,18 @@ class IPF_ORM_Relation_Parser $parser = $def['refTable']->getRelationParser(); if ( ! $parser->hasRelation($this->_table->getComponentName())) { - $parser->bind($this->_table->getComponentName(), - array('type' => IPF_ORM_Relation::ONE, - 'local' => $def['local'], - 'foreign' => $idColumnName, - 'localKey' => true, - )); + $parser->bind($this->_table->getComponentName(), null, IPF_ORM_Relation::ONE, array( + 'local' => $def['local'], + 'foreign' => $idColumnName, + 'localKey' => true, + )); } - if ( ! $this->hasRelation($def['refClass'])) { - $this->bind($def['refClass'], array('type' => IPF_ORM_Relation::MANY, - 'foreign' => $def['local'], - 'local' => $idColumnName)); + 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)) { diff --git a/ipf/orm/table.php b/ipf/orm/table.php index f938222..5a5f193 100644 --- a/ipf/orm/table.php +++ b/ipf/orm/table.php @@ -319,47 +319,29 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable return false; } - public function bind($args, $type) + public function bind($class, $alias, $type, array $options) { - $options = array(); - $options['type'] = $type; - - if ( ! isset($args[1])) { - $args[1] = array(); - } - - // the following is needed for backwards compatibility - if (is_string($args[1])) { - if ( ! isset($args[2])) { - $args[2] = array(); - } elseif (is_string($args[2])) { - $args[2] = (array) $args[2]; - } - - $classes = array_merge($this->_options['parents'], array($this->getComponentName())); + $this->_parser->bind($class, $alias, $type, $options); + } - $e = explode('.', $args[1]); - if (in_array($e[0], $classes)) { - if ($options['type'] >= IPF_ORM_Relation::MANY) { - $options['foreign'] = $e[1]; - } else { - $options['local'] = $e[1]; - } - } else { - $e2 = explode(' as ', $args[0]); - if ($e[0] !== $e2[0] && ( ! isset($e2[1]) || $e[0] !== $e2[1])) { - $options['refClass'] = $e[0]; - } + public function ownsOne($class, $alias, $options=array()) + { + $this->bind($class, $alias, IPF_ORM_Relation::ONE_COMPOSITE, $options); + } - $options['foreign'] = $e[1]; - } + public function ownsMany($class, $alias, $options=array()) + { + $this->bind($class, $alias, IPF_ORM_Relation::MANY_COMPOSITE, $options); + } - $options = array_merge($args[2], $options); - } else { - $options = array_merge($args[1], $options); - } + public function hasOne($class, $alias, $options=array()) + { + $this->bind($class, $alias, IPF_ORM_Relation::ONE_AGGREGATE, $options); + } - $this->_parser->bind($args[0], $options); + public function hasMany($class, $alias, $options=array()) + { + $this->bind($class, $alias, IPF_ORM_Relation::MANY_AGGREGATE, $options); } public function hasRelation($alias) diff --git a/ipf/session/models/_generated/BaseSession.php b/ipf/session/models/_generated/BaseSession.php index a03b6fe..e321fd7 100644 --- a/ipf/session/models/_generated/BaseSession.php +++ b/ipf/session/models/_generated/BaseSession.php @@ -18,7 +18,6 @@ abstract class BaseSession extends IPF_ORM_Record } - public static function table() { return IPF_ORM::getTable('Session'); -- 2.49.0