Skip to content

Commit 083c446

Browse files
committed
Merge pull request #420 from shmax/find-null
test for finding by null
2 parents 685f95d + 29c04fc commit 083c446

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

lib/Model.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,11 @@ protected static function get_models_from_cache(array $pks)
16451645
*/
16461646
public static function find_by_pk($values, $options)
16471647
{
1648+
if($values===null)
1649+
{
1650+
throw new RecordNotFound("Couldn't find ".get_called_class()." without an ID");
1651+
}
1652+
16481653
$table = static::table();
16491654

16501655
if($table->cache_individual_model)
@@ -1662,16 +1667,14 @@ public static function find_by_pk($values, $options)
16621667
if ($results != ($expected = count($values)))
16631668
{
16641669
$class = get_called_class();
1670+
if (is_array($values))
1671+
$values = join(',',$values);
16651672

16661673
if ($expected == 1)
16671674
{
1668-
if (!is_array($values))
1669-
$values = array($values);
1670-
1671-
throw new RecordNotFound("Couldn't find $class with ID=" . join(',',$values));
1675+
throw new RecordNotFound("Couldn't find $class with ID=$values");
16721676
}
16731677

1674-
$values = join(',',$values);
16751678
throw new RecordNotFound("Couldn't find all $class with IDs ($values) (found $results, but was looking for $expected)");
16761679
}
16771680
return $expected == 1 ? $list[0] : $list;

test/ActiveRecordFindTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,14 @@ public function test_find_by_zero()
445445
Author::find(0);
446446
}
447447

448+
/**
449+
* @expectedException ActiveRecord\RecordNotFound
450+
*/
451+
public function test_find_by_null()
452+
{
453+
Author::find(null);
454+
}
455+
448456
public function test_count_by()
449457
{
450458
$this->assert_equals(2,Venue::count_by_state('VA'));

0 commit comments

Comments
 (0)