From: Andrey Kutejko Date: Sat, 27 Jul 2013 14:19:27 +0000 (+0300) Subject: unify adding templates to a record X-Git-Tag: 0.5~128 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=d93233682902bc1adea487194c6aa9f3bb4c42a7;p=ipf.git unify adding templates to a record --- diff --git a/ipf/admin/models/_generated/BaseAdminLog.php b/ipf/admin/models/_generated/BaseAdminLog.php index 930e5eb..e71aaed 100644 --- a/ipf/admin/models/_generated/BaseAdminLog.php +++ b/ipf/admin/models/_generated/BaseAdminLog.php @@ -1,8 +1,11 @@ hasColumn('object_repr', 'string', 200, array('type' => 'string', 'length' => '200')); $this->hasColumn('action_flag', 'integer', null, array('type' => 'integer')); $this->hasColumn('change_message', 'string', 200, array('type' => 'string', 'length' => '200')); - - $this->index('idx_object_id', array('fields' => 'object_id')); $this->index('idx_object_class', array('fields' => 'object_class')); $this->index('idx_created_at', array('fields' => 'created_at')); @@ -24,11 +25,22 @@ abstract class BaseAdminLog extends IPF_ORM_Record $this->option('type', 'INNODB'); $this->option('collate', 'utf8_unicode_ci'); $this->option('charset', 'utf8'); + } public function setUp() { $timestampable0 = new IPF_ORM_Template_Timestampable(array('updated' => array('disabled' => true))); - $this->actAs($timestampable0); + $this->getTable()->addTemplate($timestampable0); + } + + public static function table() + { + return IPF_ORM::getTable('AdminLog'); + } + + public static function query($alias='') + { + return IPF_ORM::getTable('AdminLog')->createQuery($alias); } } \ No newline at end of file diff --git a/ipf/auth/models/_generated/BasePermission.php b/ipf/auth/models/_generated/BasePermission.php index 683a3b6..351ce32 100644 --- a/ipf/auth/models/_generated/BasePermission.php +++ b/ipf/auth/models/_generated/BasePermission.php @@ -1,18 +1,21 @@ setTableName('auth_permission'); $this->hasColumn('name', 'string', 255, array('unique' => true, 'type' => 'string', 'length' => '255')); - $this->option('type', 'INNODB'); $this->option('collate', 'utf8_unicode_ci'); $this->option('charset', 'utf8'); + } public function setUp() @@ -20,6 +23,7 @@ abstract class BasePermission extends IPF_ORM_Record $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')); @@ -30,4 +34,14 @@ abstract class BasePermission extends IPF_ORM_Record $this->hasMany('UserPermission', array('local' => 'id', 'foreign' => 'permission_id')); } -} + + public static function table() + { + return IPF_ORM::getTable('Permission'); + } + + public static function query($alias='') + { + return IPF_ORM::getTable('Permission')->createQuery($alias); + } +} \ No newline at end of file diff --git a/ipf/auth/models/_generated/BaseRole.php b/ipf/auth/models/_generated/BaseRole.php index a32dd81..8eef347 100644 --- a/ipf/auth/models/_generated/BaseRole.php +++ b/ipf/auth/models/_generated/BaseRole.php @@ -1,29 +1,32 @@ setTableName('auth_role'); - $this->hasColumn('name', 'string', 255, array('unique' => true, 'notblank' => true, 'type' => 'string', 'length' => '255')); - + $this->hasColumn('name', 'string', 255, array('unique' => true, 'type' => 'string', 'notblank' => true, 'length' => '255')); $this->option('type', 'INNODB'); $this->option('collate', 'utf8_unicode_ci'); $this->option('charset', 'utf8'); + } public function setUp() { - $this->hasMany('User as Users', array('refClass' => 'UserRole', - 'local' => 'role_id', - 'foreign' => 'user_id')); $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')); @@ -31,4 +34,14 @@ abstract class BaseRole extends IPF_ORM_Record $this->hasMany('UserRole', array('local' => 'id', 'foreign' => 'role_id')); } -} + + public static function table() + { + return IPF_ORM::getTable('Role'); + } + + public static function query($alias='') + { + return IPF_ORM::getTable('Role')->createQuery($alias); + } +} \ No newline at end of file diff --git a/ipf/auth/models/_generated/BaseRolePermission.php b/ipf/auth/models/_generated/BaseRolePermission.php index 2cef6dd..084fed0 100644 --- a/ipf/auth/models/_generated/BaseRolePermission.php +++ b/ipf/auth/models/_generated/BaseRolePermission.php @@ -1,8 +1,11 @@ setTableName('auth_role_permission'); $this->hasColumn('role_id', 'integer', null, array('type' => 'integer', 'primary' => true)); $this->hasColumn('permission_id', 'integer', null, array('type' => 'integer', 'primary' => true)); - $this->option('type', 'INNODB'); $this->option('collate', 'utf8_unicode_ci'); $this->option('charset', 'utf8'); + } public function setUp() @@ -26,4 +29,14 @@ abstract class BaseRolePermission extends IPF_ORM_Record 'foreign' => 'id', 'onDelete' => 'CASCADE')); } -} + + public static function table() + { + return IPF_ORM::getTable('RolePermission'); + } + + public static function query($alias='') + { + return IPF_ORM::getTable('RolePermission')->createQuery($alias); + } +} \ No newline at end of file diff --git a/ipf/auth/models/_generated/BaseUser.php b/ipf/auth/models/_generated/BaseUser.php index 85e5e88..296e39a 100644 --- a/ipf/auth/models/_generated/BaseUser.php +++ b/ipf/auth/models/_generated/BaseUser.php @@ -1,8 +1,11 @@ hasColumn('is_active', 'boolean', null, array('type' => 'boolean', 'notnull' => true, 'default' => false)); $this->hasColumn('is_superuser', 'boolean', null, array('type' => 'boolean', 'notnull' => true, 'default' => false)); $this->hasColumn('last_login', 'timestamp', null, array('type' => 'timestamp')); - $this->option('type', 'INNODB'); $this->option('collate', 'utf8_unicode_ci'); $this->option('charset', 'utf8'); + } public function setUp() @@ -40,6 +43,16 @@ abstract class BaseUser extends IPF_ORM_Record 'foreign' => 'user_id')); $timestampable0 = new IPF_ORM_Template_Timestampable(); - $this->actAs($timestampable0); + $this->getTable()->addTemplate($timestampable0); + } + + public static function table() + { + return IPF_ORM::getTable('User'); + } + + public static function query($alias='') + { + return IPF_ORM::getTable('User')->createQuery($alias); } } \ No newline at end of file diff --git a/ipf/auth/models/_generated/BaseUserPermission.php b/ipf/auth/models/_generated/BaseUserPermission.php index 738077c..9cfd2e3 100644 --- a/ipf/auth/models/_generated/BaseUserPermission.php +++ b/ipf/auth/models/_generated/BaseUserPermission.php @@ -1,8 +1,11 @@ setTableName('auth_user_permission'); $this->hasColumn('user_id', 'integer', null, array('type' => 'integer', 'primary' => true)); $this->hasColumn('permission_id', 'integer', null, array('type' => 'integer', 'primary' => true)); - $this->option('type', 'INNODB'); $this->option('collate', 'utf8_unicode_ci'); $this->option('charset', 'utf8'); + } public function setUp() @@ -26,4 +29,14 @@ abstract class BaseUserPermission extends IPF_ORM_Record 'foreign' => 'id', 'onDelete' => 'CASCADE')); } -} + + public static function table() + { + return IPF_ORM::getTable('UserPermission'); + } + + public static function query($alias='') + { + return IPF_ORM::getTable('UserPermission')->createQuery($alias); + } +} \ No newline at end of file diff --git a/ipf/auth/models/_generated/BaseUserRole.php b/ipf/auth/models/_generated/BaseUserRole.php index adb583b..24d4732 100644 --- a/ipf/auth/models/_generated/BaseUserRole.php +++ b/ipf/auth/models/_generated/BaseUserRole.php @@ -1,8 +1,11 @@ setTableName('auth_user_role'); $this->hasColumn('user_id', 'integer', null, array('type' => 'integer', 'primary' => true)); $this->hasColumn('role_id', 'integer', null, array('type' => 'integer', 'primary' => true)); - $this->option('type', 'INNODB'); $this->option('collate', 'utf8_unicode_ci'); $this->option('charset', 'utf8'); + } public function setUp() @@ -26,4 +29,14 @@ abstract class BaseUserRole extends IPF_ORM_Record 'foreign' => 'id', 'onDelete' => 'CASCADE')); } -} + + public static function table() + { + return IPF_ORM::getTable('UserRole'); + } + + public static function query($alias='') + { + return IPF_ORM::getTable('UserRole')->createQuery($alias); + } +} \ No newline at end of file diff --git a/ipf/orm/import/builder.php b/ipf/orm/import/builder.php index 3b34351..2f6d6ae 100644 --- a/ipf/orm/import/builder.php +++ b/ipf/orm/import/builder.php @@ -228,12 +228,12 @@ class IPF_ORM_Import_Builder if (is_array($options) && !empty($options)) { $optionsPhp = self::varExport($options); - $build .= " \$this->loadTemplate('" . $name . "', " . $optionsPhp . ");" . PHP_EOL; + $build .= " \$this->getTable()->addTemplate('" . $name . "', " . $optionsPhp . ");" . PHP_EOL; } else { if (isset($templates[0])) { - $build .= " \$this->loadTemplate('" . $options . "');" . PHP_EOL; + $build .= " \$this->getTable()->addTemplate('" . $options . "');" . PHP_EOL; } else { - $build .= " \$this->loadTemplate('" . $name . "');" . PHP_EOL; + $build .= " \$this->getTable()->addTemplate('" . $name . "');" . PHP_EOL; } } } @@ -258,7 +258,7 @@ class IPF_ORM_Import_Builder private function emitActAs($level, $name) { - return " \$this->actAs(\$" . strtolower($name) . "$level);" . PHP_EOL; + return " \$this->getTable()->addTemplate(\$" . strtolower($name) . "$level);" . PHP_EOL; } private function buildActAs($actAs) diff --git a/ipf/orm/record/abstract.php b/ipf/orm/record/abstract.php index 2bef507..f34c98c 100644 --- a/ipf/orm/record/abstract.php +++ b/ipf/orm/record/abstract.php @@ -114,11 +114,6 @@ abstract class IPF_ORM_Record_Abstract extends IPF_ORM_Access } } - public function loadTemplate($template, array $options = array()) - { - $this->actAs($template, $options); - } - public function bindQueryParts(array $queryParts) { $this->_table->bindQueryParts($queryParts); @@ -131,35 +126,6 @@ abstract class IPF_ORM_Record_Abstract extends IPF_ORM_Access $this->_table->addGenerator($generator, get_class($generator)); } - public function actAs($tpl, array $options = array()) - { - if ( ! is_object($tpl)) { - $className = 'IPF_ORM_Template_' . $tpl; - - if (class_exists($className, true)) { - $tpl = new $className($options); - } else if (class_exists($tpl, true)) { - $tpl = new $tpl($options); - } else { - throw new IPF_ORM_Record_Exception('Could not load behavior named: "' . $tpl . '"'); - } - } - - if ( ! ($tpl instanceof IPF_ORM_Template)) { - throw new IPF_ORM_Record_Exception('Loaded behavior class is not an istance of IPF_ORM_Template.'); - } - - $className = get_class($tpl); - - $this->_table->addTemplate($className, $tpl); - - $tpl->setTable($this->_table); - $tpl->setUp(); - $tpl->setTableDefinition(); - - return $this; - } - public function check($constraint, $name = null) { if (is_array($constraint)) { diff --git a/ipf/orm/table.php b/ipf/orm/table.php index 73ce2a4..5328b9f 100644 --- a/ipf/orm/table.php +++ b/ipf/orm/table.php @@ -997,11 +997,30 @@ class IPF_ORM_Table extends IPF_ORM_Configurable implements Countable return isset($this->_templates[$template]); } - public function addTemplate($template, IPF_ORM_Template $impl) + public function addTemplate($tpl, array $options=array()) { - $this->_templates[$template] = $impl; + if (!is_object($tpl)) { + $className = 'IPF_ORM_Template_' . $tpl; - return $this; + if (class_exists($className, true)) { + $tpl = new $className($options); + } else if (class_exists($tpl, true)) { + $tpl = new $tpl($options); + } else { + throw new IPF_ORM_Record_Exception('Could not load behavior named: "' . $tpl . '"'); + } + } + + if (!($tpl instanceof IPF_ORM_Template)) { + throw new IPF_ORM_Record_Exception('Loaded behavior class is not an istance of IPF_ORM_Template.'); + } + + $className = get_class($tpl); + $this->_templates[$className] = $tpl; + + $tpl->setTable($this); + $tpl->setUp(); + $tpl->setTableDefinition(); } public function getGenerators() diff --git a/ipf/session/models/_generated/BaseSession.php b/ipf/session/models/_generated/BaseSession.php index b126310..760b01a 100644 --- a/ipf/session/models/_generated/BaseSession.php +++ b/ipf/session/models/_generated/BaseSession.php @@ -1,8 +1,11 @@ hasColumn('expire_data', 'timestamp', null, array('type' => 'timestamp')); } + + + public static function table() + { + return IPF_ORM::getTable('Session'); + } + + public static function query($alias='') + { + return IPF_ORM::getTable('Session')->createQuery($alias); + } } \ No newline at end of file