Skip to content

Commit

Permalink
Add ArrayAccess to ORM properties
Browse files Browse the repository at this point in the history
  • Loading branch information
treffynnon committed Jan 15, 2013
1 parent 8964186 commit 598927d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
37 changes: 30 additions & 7 deletions idiorm.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
*
*/

class ORM {
class ORM implements ArrayAccess {

// ----------------------- //
// --- CLASS CONSTANTS --- //
Expand Down Expand Up @@ -1609,24 +1609,47 @@ public function delete_many() {
}

// --------------------- //
// --- 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);
}
}

Expand Down Expand Up @@ -1737,4 +1760,4 @@ protected function _str_replace_outside_quotes_cb($matches) {
/**
* A placeholder for exceptions eminating from the IdiormString class
*/
class IdiormStringException extends Exception {}
class IdiormStringException extends Exception {}
7 changes: 7 additions & 0 deletions test/test_queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 598927d

Please sign in to comment.