Skip to content

Commit 35c2e28

Browse files
greutkoenpunt
authored andcommitted
Auto setter for relations with tests (thanks to Jan →).
Support for subclasses of the model specified. null is also a valid value to be set for relations
1 parent da72f40 commit 35c2e28

3 files changed

Lines changed: 84 additions & 1 deletion

File tree

lib/Model.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,30 @@ public function __set($name, $value)
446446
return $this->$item['to']->$delegated_name = $value;
447447
}
448448

449+
$table = static::table();
450+
if ($relationship = $table->get_relationship($name)
451+
)
452+
{
453+
if (is_null($value))
454+
{
455+
$this->__relationships[$name] = $value;
456+
return $this->assign_attribute($relationship->foreign_key[0], $value);
457+
}
458+
elseif (($value instanceof \ActiveRecord\Model) &&
459+
$value instanceof $relationship->class_name)
460+
{
461+
$this->__relationships[$name] = $value;
462+
$pk = $value->get_primary_key(0);
463+
return $this->assign_attribute(
464+
$relationship->foreign_key[0],
465+
$value->is_new_record() ? null : $value->{$pk[0]}
466+
);
467+
}
468+
else
469+
{
470+
throw new RelationshipException();
471+
}
472+
}
449473
throw new UndefinedPropertyException(get_called_class(),$name);
450474
}
451475

@@ -837,7 +861,7 @@ public static function table()
837861
* @param boolean $guard_attributes Set to true to guard protected/non-accessible attributes
838862
* @return Model
839863
*/
840-
public static function create($attributes, $validate=true, $guard_attributes=true)
864+
public static function create(array $attributes=array(), $validate=true, $guard_attributes=true)
841865
{
842866
$class_name = get_called_class();
843867
$model = new $class_name($attributes, $guard_attributes);

test/RelationshipTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,4 +726,57 @@ public function test_dont_attempt_eager_load_when_record_does_not_exist()
726726
{
727727
Author::find(999999, array('include' => array('books')));
728728
}
729+
730+
public function test_gh_125_auto_setter_for_relation()
731+
{
732+
$event = new Event(array(
733+
'venue' => new Venue()
734+
));
735+
736+
$this->assert_not_null($event->venue);
737+
$this->assert_null($event->venue_id);
738+
}
739+
740+
public function test_gh_125_auto_setter_for_relation_with_id()
741+
{
742+
$event = new Event(array(
743+
'venue' => Venue::create()
744+
));
745+
746+
$this->assert_not_null($event->venue);
747+
$this->assert_not_null($event->venue_id);
748+
$event->venue->delete();
749+
}
750+
751+
/**
752+
* @expectedException ActiveRecord\RelationshipException
753+
*/
754+
public function test_gh_125_auto_setter_for_relation_raises_exception()
755+
{
756+
$event = new Event(array(
757+
'venue' => new Book()
758+
));
759+
}
760+
761+
public function test_gh_125_auto_setter_for_relation_using_subclass()
762+
{
763+
$event = new Event(array(
764+
'venue' => new SubVenue()
765+
));
766+
767+
$this->assert_not_null($event->venue);
768+
$this->assert_null($event->venue_id);
769+
}
770+
771+
public function test_gh_125_unset_existing_relation()
772+
{
773+
$event = new Event(array(
774+
'venue' => Venue::create()
775+
));
776+
777+
$event->venue->delete();
778+
$event->venue = null;
779+
$this->assert_null($event->venue);
780+
$this->assert_null($event->venue_id);
781+
}
729782
}

test/models/SubVenue.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
class SubVenue extends Venue
3+
{
4+
static $table_name = 'venues';
5+
}
6+
?>

0 commit comments

Comments
 (0)