From 65c1c62270f20ec5c3efe73e8c0cec85cce40cc4 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Mon, 31 May 2010 15:34:25 -0400 Subject: [PATCH] Add shorthand column creation methods Credit to http://github.com/maintainable/framework/commit/7709643d3b88cff39cec717d4c6248ebd373a480 --- .../Db/lib/Horde/Db/Adapter/Base/ColumnDefinition.php | 1 - .../Db/lib/Horde/Db/Adapter/Base/TableDefinition.php | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/framework/Db/lib/Horde/Db/Adapter/Base/ColumnDefinition.php b/framework/Db/lib/Horde/Db/Adapter/Base/ColumnDefinition.php index 69c405e02..9d47ad6a4 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Base/ColumnDefinition.php +++ b/framework/Db/lib/Horde/Db/Adapter/Base/ColumnDefinition.php @@ -255,5 +255,4 @@ class Horde_Db_Adapter_Base_ColumnDefinition array_merge($options, array('column' => $this)) ); } - } diff --git a/framework/Db/lib/Horde/Db/Adapter/Base/TableDefinition.php b/framework/Db/lib/Horde/Db/Adapter/Base/TableDefinition.php index b3a8e6b6a..005422dc5 100644 --- a/framework/Db/lib/Horde/Db/Adapter/Base/TableDefinition.php +++ b/framework/Db/lib/Horde/Db/Adapter/Base/TableDefinition.php @@ -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() -- 2.11.0