File tree Expand file tree Collapse file tree 6 files changed +15
-5
lines changed Expand file tree Collapse file tree 6 files changed +15
-5
lines changed Original file line number Diff line number Diff line change @@ -173,7 +173,11 @@ private function check_include()
173173 try {
174174 $ assoc = $ this ->model ->$ association ;
175175
176- if (!is_array ($ assoc ))
176+ if ($ assoc === null )
177+ {
178+ $ this ->attributes [$ association ] = null ;
179+ }
180+ elseif (!is_array ($ assoc ))
177181 {
178182 $ serialized = new $ serializer_class ($ assoc , $ options );
179183 $ this ->attributes [$ association ] = $ serialized ->to_a ();;
Original file line number Diff line number Diff 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 ' ));
Original file line number Diff line number Diff line change @@ -38,7 +38,7 @@ CREATE TABLE venues (
3838
3939CREATE 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 ),
Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ CREATE TABLE venues (
4141CREATE SEQUENCE events_seq ;
4242CREATE 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 ),
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ CREATE TABLE venues (
3737
3838CREATE 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 ),
Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ CREATE TABLE venues (
3737
3838CREATE 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 ),
You can’t perform that action at this time.
0 commit comments