Add shorthand column creation methods
authorChuck Hagenbuch <chuck@horde.org>
Mon, 31 May 2010 19:34:25 +0000 (15:34 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Mon, 31 May 2010 19:34:25 +0000 (15:34 -0400)
Credit to http://github.com/maintainable/framework/commit/7709643d3b88cff39cec717d4c6248ebd373a480

framework/Db/lib/Horde/Db/Adapter/Base/ColumnDefinition.php
framework/Db/lib/Horde/Db/Adapter/Base/TableDefinition.php

index 69c405e..9d47ad6 100644 (file)
@@ -255,5 +255,4 @@ class Horde_Db_Adapter_Base_ColumnDefinition
             array_merge($options, array('column' => $this))
         );
     }
-
 }
index b3a8e6b..005422d 100644 (file)
@@ -29,6 +29,9 @@ class Horde_Db_Adapter_Base_TableDefinition implements ArrayAccess, IteratorAggr
     protected $_columns = null;
     protected $_primaryKey = null;
 
+    protected $_columntypes = array('string', 'text', 'integer', 'float',
+        'datetime', 'timestamp', 'time', 'date', 'binary', 'boolean');
+
     /**
      * Class Constructor
      *
@@ -137,6 +140,21 @@ class Horde_Db_Adapter_Base_TableDefinition implements ArrayAccess, IteratorAggr
     }
 
     /**
+     * Use __call to provide shorthand column creation ($this->integer(), etc.)
+     */
+    public function __call($method, $arguments)
+    {
+        if (!in_array($method, $this->_columntypes)) {
+            throw new BadMethodCallException('Call to undeclared method "'.$method.'"');
+        } elseif (count($arguments) > 0 && count($arguments) < 3) {
+            return $this->column($arguments[0], $method,
+                                 isset($arguments[1]) ? $arguments[1] : array());
+        } else {
+            throw new BadMethodCallException('Method "'.$method.'" takes two arguments');
+        }
+    }
+
+    /**
      * Wrap up table creation block & create the table
      */
     public function end()