Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potential fix for issue #546 #548

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
}

// 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;
}
}
23 changes: 16 additions & 7 deletions test/RelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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();
Expand Down