From fdc425471989c6b80702bb81fedf225c82222cf6 Mon Sep 17 00:00:00 2001 From: Kyle Decot Date: Fri, 11 May 2012 15:22:56 -0400 Subject: [PATCH] You can now do something like Object::find(123); --- db_object.php | 10 +++++++++- test/DbObjectTest.php | 5 +++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/db_object.php b/db_object.php index 7c41533..64da178 100644 --- a/db_object.php +++ b/db_object.php @@ -1136,7 +1136,15 @@ public static function find($columns, $table='') { if (!$object->null_instantiated) { throw new Exception("The method: 'find' cannot be called on an existing object, it must be null instantiated."); } - + + // Column can either be an array OR it can simply + // be the ID of the object we're looking for... + + if (!is_array($columns)) { + $primary_key = $object->primary_key_field; + $columns = array($primary_key => (int)$columns); + } + foreach ($columns as $field_name => $field_value) { // Make sure it's a legit attribute if (!$object->is_acceptable_attribute($field_name)) { diff --git a/test/DbObjectTest.php b/test/DbObjectTest.php index ea8e1de..f5a9d0e 100644 --- a/test/DbObjectTest.php +++ b/test/DbObjectTest.php @@ -883,6 +883,11 @@ function testFindWhenUsedNormally() $animal = db_object::find(array('name' => 'Cow'), 'animals'); $this->assertEquals(2, $animal->get_id()); } + + function testFindUsingId() { + $farm = farm::find(1); + $this->assertEquals(1, $farm->get_id()); + } function testExtendedFind() { $farm = farm::find(array('name' => 'eSchool Farms'));