summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.markdown8
-rw-r--r--idiorm.php15
-rw-r--r--test/test_queries.php6
3 files changed, 24 insertions, 5 deletions
diff --git a/README.markdown b/README.markdown
index 9ebb1b9..cccccd3 100644
--- a/README.markdown
+++ b/README.markdown
@@ -337,7 +337,7 @@ The `as_array` method takes column names as optional arguments. If one or more o
### 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:
+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. By using the `set` method it is also possible to update multiple properties at once, by passing in an associative array:
$person = ORM::for_table('person')->find_one(5);
@@ -345,6 +345,12 @@ To update the database, change one or more of the properties of the object, then
$person->set('name', 'Bob Smith');
$person->age = 20;
+ // This is equivalent to the above two assignments
+ $person->set(array(
+ 'name' => 'Bob Smith',
+ 'age' => 20,
+ ));
+
// Syncronise the object with the database
$person->save();
diff --git a/idiorm.php b/idiorm.php
index 2c75776..0296176 100644
--- a/idiorm.php
+++ b/idiorm.php
@@ -1031,12 +1031,19 @@
/**
* Set a property to a particular value on this object.
- * Flags that property as 'dirty' so it will be saved to the
+ * To set multiple properties at once, pass an associative array
+ * as the first parameter and leave out the second parameter.
+ * Flags the properties as 'dirty' so they will be saved to the
* database when save() is called.
*/
- public function set($key, $value) {
- $this->_data[$key] = $value;
- $this->_dirty_fields[$key] = $value;
+ public function set($key, $value = null) {
+ if (!is_array($key)) {
+ $key = array($key => $value);
+ }
+ foreach ($key as $field => $value) {
+ $this->_data[$field] = $value;
+ $this->_dirty_fields[$field] = $value;
+ }
}
/**
diff --git a/test/test_queries.php b/test/test_queries.php
index 217b7fd..0faa592 100644
--- a/test/test_queries.php
+++ b/test/test_queries.php
@@ -214,6 +214,12 @@
Tester::check_equal("Update data", $expected);
$widget = ORM::for_table('widget')->find_one(1);
+ $widget->set(array("name" => "Fred", "age" => 10));
+ $widget->save();
+ $expected = "UPDATE `widget` SET `name` = 'Fred', `age` = '10' WHERE `id` = '1'";
+ Tester::check_equal("Update multiple fields", $expected);
+
+ $widget = ORM::for_table('widget')->find_one(1);
$widget->delete();
$expected = "DELETE FROM `widget` WHERE `id` = '1'";
Tester::check_equal("Delete data", $expected);