summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown16
-rw-r--r--idiorm.php14
2 files changed, 30 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown
index f4ed43f..1144a43 100644
--- a/README.markdown
+++ b/README.markdown
@@ -263,6 +263,22 @@ Once you've got a set of records (objects) back from a query, you can access pro
$name = $person->get('name');
$name = $person->name;
+You can also get the all the data wrapped by an ORM instance using the `as_array` method. This will return an associative array mapping column names (keys) to their values.
+
+The `as_array` method takes column names as optional arguments. If one or more of these arguments is supplied, only matching column names will be returned.
+
+ $person = ORM::for_table('person')->create();
+
+ $person->first_name = 'Fred';
+ $person->surname = 'Bloggs';
+ $person->age = 50;
+
+ // Returns array('first_name' => 'Fred', 'surname' => 'Bloggs', 'age' => 50)
+ $data = $person->as_array();
+
+ // Returns array('first_name' => 'Fred', 'age' => 50)
+ $data = $person->as_array('first_name', 'age');
+
### Updating records ###
To update the database, change one or more of the properties of the object, then call the `save` method to commit the changes to the database. Again, you can change the values of the object's properties either by using the `set` method or by setting the value of the property directly:
diff --git a/idiorm.php b/idiorm.php
index 58de3b9..e4e9ab6 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -807,6 +807,20 @@
}
/**
+ * Return the raw data wrapped by this ORM
+ * instance as an associative array. Column
+ * names may optionally be supplied as arguments,
+ * if so, only those keys will be returned.
+ */
+ public function as_array() {
+ if (func_num_args() === 0) {
+ return $this->_data;
+ }
+ $args = func_get_args();
+ return array_intersect_key($this->_data, array_flip($args));
+ }
+
+ /**
* Return the value of a property of this object (database row)
* or null if not present.
*/