Skip to content

Commit ddce812

Browse files
Merge branch 'master' of git://github.com/jpfuentes2/php-activerecord into 1.1-dev
2 parents 0e1f823 + 54682fa commit ddce812

File tree

8 files changed

+31
-15
lines changed

8 files changed

+31
-15
lines changed

lib/Model.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,6 +1722,11 @@ protected static function get_models_from_cache(array $pks)
17221722
*/
17231723
public static function find_by_pk($values, $options)
17241724
{
1725+
if($values===null)
1726+
{
1727+
throw new RecordNotFound("Couldn't find ".get_called_class()." without an ID");
1728+
}
1729+
17251730
$table = static::table();
17261731

17271732
if($table->cache_individual_model)
@@ -1739,16 +1744,14 @@ public static function find_by_pk($values, $options)
17391744
if ($results != ($expected = count($values)))
17401745
{
17411746
$class = get_called_class();
1747+
if (is_array($values))
1748+
$values = join(',',$values);
17421749

17431750
if ($expected == 1)
17441751
{
1745-
if (!is_array($values))
1746-
$values = array($values);
1747-
1748-
throw new RecordNotFound("Couldn't find $class with ID=" . join(',',$values));
1752+
throw new RecordNotFound("Couldn't find $class with ID=$values");
17491753
}
17501754

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

lib/Serialization.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ private function check_include()
176176
try {
177177
$assoc = $this->model->$association;
178178

179-
if (!is_array($assoc))
179+
if ($assoc === null)
180+
{
181+
$this->attributes[$association] = null;
182+
}
183+
elseif (!is_array($assoc))
180184
{
181185
$serialized = new $serializer_class($assoc, $options);
182186
$this->attributes[$association] = $serialized->to_a();;

test/ActiveRecordFindTest.php

Lines changed: 8 additions & 5 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'));
@@ -460,11 +468,6 @@ public function test_find_by_pk_should_not_use_limit()
460468

461469
public function test_find_by_datetime()
462470
{
463-
if ( getenv('TRAVIS') ) $this->markTestSkipped(
464-
'The Travis CI environment seems to screw this up for unknonwn reasons; ' .
465-
'see Github #298 (https://github.com/kla/php-activerecord/issues/298)'
466-
);
467-
468471
$now = new DateTime();
469472
$arnow = new ActiveRecord\DateTime();
470473
$arnow->setTimestamp($now->getTimestamp());

test/RelationshipTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ public function test_belongs_to_returns_null_when_no_record()
133133
$this->assert_null($event->venue);
134134
}
135135

136+
public function test_belongs_to_returns_null_when_foreign_key_is_null()
137+
{
138+
$event = Event::create(array('title' => 'venueless event'));
139+
$this->assert_null($event->venue);
140+
}
141+
136142
public function test_belongs_to_with_explicit_class_name()
137143
{
138144
Event::$belongs_to = array(array('explicit_class_name', 'class_name' => 'Venue'));

test/sql/mysql.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ CREATE TABLE venues (
3838

3939
CREATE TABLE events (
4040
id int NOT NULL auto_increment PRIMARY KEY,
41-
venue_id int NOT NULL,
41+
venue_id int NULL,
4242
host_id int NOT NULL,
4343
title varchar(60) NOT NULL,
4444
description varchar(50),

test/sql/oci.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ CREATE TABLE venues (
4141
CREATE SEQUENCE events_seq;
4242
CREATE TABLE events (
4343
id INT NOT NULL PRIMARY KEY,
44-
venue_id int NOT NULL,
44+
venue_id int NULL,
4545
host_id int NOT NULL,
4646
title varchar(60) NOT NULL,
4747
description varchar(10),

test/sql/pgsql.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ CREATE TABLE venues (
3737

3838
CREATE TABLE events (
3939
id SERIAL PRIMARY KEY,
40-
venue_id int NOT NULL,
40+
venue_id int NULL,
4141
host_id int NOT NULL,
4242
title varchar(60) NOT NULL,
4343
description varchar(10),

test/sql/sqlite.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ CREATE TABLE venues (
3737

3838
CREATE TABLE events (
3939
id INTEGER NOT NULL PRIMARY KEY,
40-
venue_id int NOT NULL,
40+
venue_id int NULL,
4141
host_id int NOT NULL,
4242
title varchar(60) NOT NULL,
4343
description varchar(10),

0 commit comments

Comments
 (0)