From: Andrey Kutejko Date: Fri, 19 Apr 2013 22:55:29 +0000 (+0300) Subject: orderable template X-Git-Tag: 0.6~134 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=e0d9b7fbbc2a27d96f7d3b7a13e02d471f958805;p=ipf-legacy-orm.git orderable template --- diff --git a/ipf/orm/record.php b/ipf/orm/record.php index 402b660..3510d9d 100644 --- a/ipf/orm/record.php +++ b/ipf/orm/record.php @@ -1265,7 +1265,8 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab return (string) $this->_oid; } - public function ModelAdmin(){ + public function ModelAdmin() + { $cn = get_class($this); if (isset(IPF_Admin_Model::$models[$cn])) return IPF_Admin_Model::$models[$cn]; @@ -1291,22 +1292,26 @@ abstract class IPF_ORM_Record extends IPF_ORM_Record_Abstract implements Countab } } - public function SetCustom($name, $val){ + public function SetCustom($name, $val) + { $this->_custom[$name] = $val; } - public function GetCustom($name){ + public function GetCustom($name) + { if (isset($this->_custom[$name])) return $this->_custom[$name]; return null; } - public function _reorder($ids, $ord_field, $drop_id, $prev_ids, $ord=1){ - foreach($ids as $id){ + public function _reorder($ids, $ord_field, $drop_id, $prev_ids, $ord=1) + { + foreach($ids as $id) { $item = $this->getTable()->find($id); $item[$ord_field] = $ord; $item->save(); $ord++; } } -} \ No newline at end of file +} + diff --git a/ipf/orm/template.php b/ipf/orm/template.php index 3de6e4d..9bca51f 100644 --- a/ipf/orm/template.php +++ b/ipf/orm/template.php @@ -4,6 +4,7 @@ abstract class IPF_ORM_Template extends IPF_ORM_Record_Abstract { protected $_invoker; protected $_plugin; + public function setTable(IPF_ORM_Table $table) { $this->_table = $table; @@ -27,7 +28,6 @@ abstract class IPF_ORM_Template extends IPF_ORM_Record_Abstract public function addChild(IPF_ORM_Template $template) { $this->_plugin->addChild($template); - return $this; } @@ -48,11 +48,10 @@ abstract class IPF_ORM_Template extends IPF_ORM_Record_Abstract public function setUp() { - } public function setTableDefinition() { - } -} \ No newline at end of file +} + diff --git a/ipf/orm/template/listener/orderable.php b/ipf/orm/template/listener/orderable.php new file mode 100644 index 0000000..8b210e1 --- /dev/null +++ b/ipf/orm/template/listener/orderable.php @@ -0,0 +1,38 @@ +columnName = $columnName; + } + + public function preInsert(IPF_ORM_Event $event) + { + $this->setOrderValue($event->getInvoker()); + } + + public function preUpdate(IPF_ORM_Event $event) + { + $this->setOrderValue($event->getInvoker()); + } + + private function setOrderValue($obj) + { + $columnName = $this->columnName; + if ($obj->$columnName) + return; + + $res = IPF_ORM_Query::create() + ->select('max('.$this->columnName.') as x_ord') + ->from(get_class($obj)) + ->execute(); + if (isset($res[0]->x_ord)) + $obj->$columnName = (int)$res[0]->x_ord + 1; + else + $obj->$columnName = 1; + } +} + diff --git a/ipf/orm/template/orderable.php b/ipf/orm/template/orderable.php new file mode 100644 index 0000000..2f95bf7 --- /dev/null +++ b/ipf/orm/template/orderable.php @@ -0,0 +1,24 @@ +columnName = $options['name']; + } + + public function getColumnName() + { + return $this->columnName; + } + + public function setTableDefinition() + { + $this->hasColumn($this->columnName, 'integer', null, ''); + $this->addListener(new IPF_ORM_Template_Listener_Orderable($this->columnName)); + } +} +