diff options
author | Simon Holywell <[email protected]> | 2013-01-15 09:11:43 +0000 |
---|---|---|
committer | Simon Holywell <[email protected]> | 2013-01-15 09:11:43 +0000 |
commit | 598927df4c32015e9b2a7a7eecb04eeda0a9dc3b (patch) | |
tree | 622bf43248bce5c486098d0db136f4f713c9745c | |
parent | 8964186a5997a2071c9afcf2c9cbebc69446956f (diff) |
Add ArrayAccess to ORM properties
-rw-r--r-- | idiorm.php | 37 | ||||
-rwxr-xr-x | test/test_queries.php | 7 |
2 files changed, 37 insertions, 7 deletions
@@ -38,7 +38,7 @@ * */ - class ORM { + class ORM implements ArrayAccess { // ----------------------- // // --- CLASS CONSTANTS --- // @@ -1609,24 +1609,47 @@ } // --------------------- // - // --- MAGIC METHODS --- // + // --- ArrayAccess --- // // --------------------- // - public function __get($key) { + + public function offsetExists($key) { + return isset($this->_data[$key]); + } + + public function offsetGet($key) { return $this->get($key); } - public function __set($key, $value) { + public function offsetSet($key, $value) { + if(is_null($key)) { + throw new InvalidArgumentException('You must specify a key/array index.'); + } $this->set($key, $value); } - public function __unset($key) { + public function offsetUnset($key) { unset($this->_data[$key]); unset($this->_dirty_fields[$key]); } + // --------------------- // + // --- MAGIC METHODS --- // + // --------------------- // + public function __get($key) { + return $this->offsetGet($key); + } + + public function __set($key, $value) { + $this->offsetSet($key, $value); + } + + public function __unset($key) { + $this->offsetUnset($key); + } + public function __isset($key) { - return isset($this->_data[$key]); + return $this->offsetExists($key); } } @@ -1737,4 +1760,4 @@ /** * A placeholder for exceptions eminating from the IdiormString class */ - class IdiormStringException extends Exception {} + class IdiormStringException extends Exception {}
\ No newline at end of file diff --git a/test/test_queries.php b/test/test_queries.php index 368af71..8580fd6 100755 --- a/test/test_queries.php +++ b/test/test_queries.php @@ -347,6 +347,13 @@ $expected = "INSERT OR IGNORE INTO `widget` (`id`, `name`) VALUES ('1', 'Tolstoy')"; Tester::check_equal("Raw execute", $expected); // A bit of a silly test, as query is passed through + $widget = ORM::for_table('widget')->create(); + $widget['name'] = "Fred"; + $widget['age'] = 10; + $widget->save(); + $expected = "INSERT INTO `widget` (`name`, `age`) VALUES ('Fred', '10')"; + Tester::check_equal("Insert data using ArrayAccess", $expected); + ORM::for_table('widget')->where('name', 'Fred')->find_one(); $statement = ORM::get_last_statement(); $test_name = 'get_last_statement() returned MockPDOStatement'; |