]> git.andy128k.dev Git - ipf-legacy-orm.git/commitdiff
add prepend option and index to orderable template.
authorAndrey Kutejko <andy128k@gmail.com>
Thu, 25 Jul 2013 18:48:11 +0000 (21:48 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Thu, 25 Jul 2013 18:48:11 +0000 (21:48 +0300)
ipf/orm/template/listener/orderable.php
ipf/orm/template/orderable.php

index 2ff3372a1937cd1ffceb23c75dc5d941cbc14542..157f49243a257538737168b3c1e04f11f4613210 100644 (file)
@@ -2,11 +2,12 @@
 
 class IPF_ORM_Template_Listener_Orderable
 {
-    private $columnName = 'ord';
+    private $columnName, $prepend;
 
-    public function __construct($columnName)
+    public function __construct($columnName, $prepend)
     {
         $this->columnName = $columnName;
+        $this->prepend = $prepend;
     }
 
     public function preInsert(IPF_ORM_Event $event)
@@ -22,17 +23,23 @@ class IPF_ORM_Template_Listener_Orderable
     private function setOrderValue($obj)
     {
         $columnName = $this->columnName;
-        if ($obj->$columnName)
+        if ($obj->$columnName !== null)
             return;
 
+        if ($this->prepend) {
+            $f = 'min';
+            $d = '-';
+        } else {
+            $f = 'max';
+            $d = '+';
+        }
+
         $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;
+            ->select('coalesce('.$f.'('.$this->columnName.') '.$d.' 1, 1) as x_ord')
+            ->from(get_class($obj))
+            ->execute();
+
+        $obj->$columnName = (int)$res[0]->x_ord;
     }
 }
 
index 327082853ca886e160730ba9e5d870a2b0b1f7cb..3d82df9cb6cdfa9fe5b1a1b80a4c94eb6230fed9 100644 (file)
@@ -4,6 +4,7 @@ class IPF_ORM_Template_Orderable extends IPF_ORM_Template
 {
     private $columnName = 'ord';
     private $exclude = true;
+    private $prepend = false;
 
     public function __construct(array $options=array())
     {
@@ -12,6 +13,8 @@ class IPF_ORM_Template_Orderable extends IPF_ORM_Template
                 $this->columnName = $options['name'];
             if (array_key_exists('exclude', $options))
                 $this->exclude = $options['exclude'];
+            if (array_key_exists('prepend', $options))
+                $this->prepend = $options['prepend'];
         }
     }
 
@@ -23,7 +26,8 @@ class IPF_ORM_Template_Orderable extends IPF_ORM_Template
     public function setTableDefinition()
     {
         $this->hasColumn($this->columnName, 'integer', null, array('exclude' => $this->exclude));
-        $this->getTable()->listeners['Orderable_'.$this->columnName] = new IPF_ORM_Template_Listener_Orderable($this->columnName);
+        $this->index($this->getTable()->getOption('tableName') . '_orderable_' . $this->columnName, array('fields' => array($this->columnName)));
+        $this->getTable()->listeners['Orderable_'.$this->columnName] = new IPF_ORM_Template_Listener_Orderable($this->columnName, $this->prepend);
     }
 }