File tree Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change @@ -685,6 +685,9 @@ public function load(Model $model)
685
685
*/
686
686
class BelongsTo extends AbstractRelationship
687
687
{
688
+
689
+ static protected $ valid_association_options = array ('primary_key ' );
690
+
688
691
public function __construct ($ options =array ())
689
692
{
690
693
parent ::__construct ($ options );
@@ -695,6 +698,9 @@ public function __construct($options=array())
695
698
//infer from class_name
696
699
if (!$ this ->foreign_key )
697
700
$ this ->foreign_key = array (Inflector::instance ()->keyify ($ this ->class_name ));
701
+
702
+ if (!isset ($ this ->primary_key ) && isset ($ this ->options ['primary_key ' ]))
703
+ $ this ->primary_key = is_array ($ this ->options ['primary_key ' ]) ? $ this ->options ['primary_key ' ] : array ($ this ->options ['primary_key ' ]);
698
704
}
699
705
700
706
public function __get ($ name )
Original file line number Diff line number Diff line change @@ -165,6 +165,19 @@ public function test_belongs_to_with_explicit_foreign_key()
165
165
Book::$ belongs_to = $ old ;
166
166
}
167
167
168
+ public function test_belongs_to_with_explicit_primary_key ()
169
+ {
170
+ $ old = Book::$ belongs_to ;
171
+ Book::$ belongs_to = array (array ('explicit_author ' , 'class_name ' => 'Author ' , 'primary_key ' => 'parent_author_id ' ));
172
+
173
+ $ book = Book::find (1 );
174
+ $ this ->assert_equals (1 , $ book ->author_id );
175
+ $ this ->assert_equals (1 , $ book ->explicit_author ->parent_author_id );
176
+ $ this ->assert_equals (3 , $ book ->explicit_author ->author_id );
177
+
178
+ Book::$ belongs_to = $ old ;
179
+ }
180
+
168
181
public function test_belongs_to_with_select ()
169
182
{
170
183
Event::$ belongs_to [0 ]['select ' ] = 'id, city ' ;
You can’t perform that action at this time.
0 commit comments