From c67ee204f6e198f05d4ebdff3b7f902033a394a9 Mon Sep 17 00:00:00 2001 From: Louis Rose Date: Tue, 5 Jul 2016 16:53:55 +0100 Subject: [PATCH 1/2] Corrects record appending for belongs_to #546 --- lib/Relationship.php | 11 ++++++++++- test/RelationshipTest.php | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/Relationship.php b/lib/Relationship.php index 297d89438..e2dbe78f0 100644 --- a/lib/Relationship.php +++ b/lib/Relationship.php @@ -725,4 +725,13 @@ public function load_eagerly($models=array(), $attributes, $includes, Table $tab { $this->query_and_attach_related_models_eagerly($table,$models,$attributes,$includes, $this->primary_key,$this->foreign_key); } -} \ No newline at end of file + + // Unlike the other relationships, a belongs_to stores its foreign key on the associate (and not + // on the new record). Therewfore, we must override the append_record_to_associate behaviour of + // AbstractRelationship to provide this behaviour. + protected function append_record_to_associate(Model $associate, Model $record) + { + $associate->{$this->foreign_key[0]} = $record->id; + return $record; + } +} diff --git a/test/RelationshipTest.php b/test/RelationshipTest.php index d3ce103d6..4eb62a86c 100644 --- a/test/RelationshipTest.php +++ b/test/RelationshipTest.php @@ -230,6 +230,15 @@ public function test_belongs_to_create_association() $this->assert_not_null($venue->id); } + public function test_belongs_to_create_association_sets_foreign_key() + { + $event = $this->get_relationship(); + $values = array('city' => 'Richmond', 'state' => 'VA', 'name' => 'Club 54', 'address' => '123 street'); + $venue = $event->create_venue($values); + + $this->assert_equals($venue->id, $event->venue_id); + } + public function test_build_association_overwrites_guarded_foreign_keys() { $author = new AuthorAttrAccessible(); From ed71d3bad78d63d53da1623e3ab29f33eea829bc Mon Sep 17 00:00:00 2001 From: Louis Rose Date: Tue, 5 Jul 2016 16:52:33 +0100 Subject: [PATCH 2/2] Removes trailing whitespace. --- lib/Relationship.php | 4 ++-- test/RelationshipTest.php | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/Relationship.php b/lib/Relationship.php index e2dbe78f0..80d6055a1 100644 --- a/lib/Relationship.php +++ b/lib/Relationship.php @@ -289,7 +289,7 @@ protected function set_class_name($class_name) if (!has_absolute_namespace($class_name) && isset($this->options['namespace'])) { $class_name = $this->options['namespace'].'\\'.$class_name; } - + $reflection = Reflections::instance()->add($class_name)->get($class_name); if (!$reflection->isSubClassOf('ActiveRecord\\Model')) @@ -505,7 +505,7 @@ public function load(Model $model) $fk = $this->foreign_key; $this->set_keys($this->get_table()->class->getName(), true); - + $class = $this->class_name; $relation = $class::table()->get_relationship($this->through); $through_table = $relation->get_table(); diff --git a/test/RelationshipTest.php b/test/RelationshipTest.php index 4eb62a86c..7b33562fc 100644 --- a/test/RelationshipTest.php +++ b/test/RelationshipTest.php @@ -23,7 +23,7 @@ public function set_up($connection_name=null) Venue::$has_one = array(); Employee::$has_one = array(array('position')); Host::$has_many = array(array('events', 'order' => 'id asc')); - + foreach ($this->relationship_names as $name) { if (preg_match("/$name/", $this->getName(), $match)) @@ -80,26 +80,26 @@ public function test_has_many_basic() { $this->assert_default_has_many($this->get_relationship()); } - + public function test_gh_256_eager_loading_three_levels_deep() { /* Before fix Undefined offset: 0 */ $conditions['include'] = array('events'=>array('host'=>array('events'))); $venue = Venue::find(2,$conditions); - + $events = $venue->events; $this->assertEquals(2,count($events)); $event_yeah_yeahs = $events[0]; $this->assertEquals('Yeah Yeah Yeahs',$event_yeah_yeahs->title); - + $event_host = $event_yeah_yeahs->host; $this->assertEquals('Billy Crystal',$event_host->name); - + $bill_events = $event_host->events; - + $this->assertEquals('Yeah Yeah Yeahs',$bill_events[0]->title); } - + /** * @expectedException ActiveRecord\RelationshipException */