Skip to content

Commit

Permalink
You can now do something like Object::find(123);
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Decot committed May 11, 2012
1 parent 0ac183b commit fdc4254
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
10 changes: 9 additions & 1 deletion db_object.php
Expand Up @@ -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)) {
Expand Down
5 changes: 5 additions & 0 deletions test/DbObjectTest.php
Expand Up @@ -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'));
Expand Down

0 comments on commit fdc4254

Please sign in to comment.