From dca3e48bb2eb188d1e88781ffb0f4acbe25fcf48 Mon Sep 17 00:00:00 2001 From: Ronald Date: Tue, 31 Mar 2015 17:06:49 +0200 Subject: [PATCH] better support for relations --- README.md | 14 +++++++ .../EloquentRepository/EloquentRepository.php | 10 ++++- .../EloquentRepository/Repository.php | 1 - .../EloquentRepositoryTest.php | 41 ++++++++++++++++++- .../EloquentRepository/MockModel.php | 8 +++- 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 595ed41..4d6eeaa 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,20 @@ These methods are available on the repository and have an api equal to the corre - where* - all other static methods +## Relations +To get relations from the repository use these methods: +```php + [Model:] [Repository:] [result] +- $model->parent $repo->getAttribute($model, 'parent'); the parent model +- $model->children $repo->getAttribute($model, 'children'); the child models as a collection +- $model->children() $repo->children($model); the child models as a relation +- $model->children()->save($child) + $repo->children($model, $child); add the child to the children of the model +- $model->children()->saveMany($list) + $repo->children($model, $list); add an array of new children to the children of the model +``` +These examples assume that you have defined parent and children as relations on the model! + ## Usage ```php $repo = new EloquentRepository('App\\Person'); diff --git a/src/DoctorBeat/EloquentRepository/EloquentRepository.php b/src/DoctorBeat/EloquentRepository/EloquentRepository.php index eba1ee1..a647c1a 100644 --- a/src/DoctorBeat/EloquentRepository/EloquentRepository.php +++ b/src/DoctorBeat/EloquentRepository/EloquentRepository.php @@ -97,7 +97,7 @@ public function getAttribute($entity, $key) { * @param Model or array() of Models $entities * @return mixed */ - public function addTo($relation, $entities) { + private function addTo($relation, $entities) { if (is_array($entities)) { return $relation->saveMany($entities); } else { @@ -122,7 +122,13 @@ public function __call($method, $parameters) { if (count($parameters) > 0 && is_object($parameters[0]) && method_exists($parameters[0], $method)) { //dynamic matching method found, use this: $instance = array_shift($parameters); - return $instance->$method($parameters); + $relation = $instance->$method(); + + //if we have more paramaters these should be added to the relationship: + if (count($parameters) > 0) { + $this->addTo($relation, $parameters[0]); + } + return $relation; } else { //rewrite any non-implemented dynamic method to a static method return call_user_func_array("{$this->modelClassname}::{$method}", $parameters); diff --git a/src/DoctorBeat/EloquentRepository/Repository.php b/src/DoctorBeat/EloquentRepository/Repository.php index b384829..3f98180 100644 --- a/src/DoctorBeat/EloquentRepository/Repository.php +++ b/src/DoctorBeat/EloquentRepository/Repository.php @@ -14,6 +14,5 @@ function touch($entity); function all(); function find($id); function getAttribute($entity, $key); - function addTo($relation, $entities); function associate($relation, $entity); } diff --git a/tests/DoctorBeat/EloquentRepository/EloquentRepositoryTest.php b/tests/DoctorBeat/EloquentRepository/EloquentRepositoryTest.php index 76f756d..aa7e57a 100644 --- a/tests/DoctorBeat/EloquentRepository/EloquentRepositoryTest.php +++ b/tests/DoctorBeat/EloquentRepository/EloquentRepositoryTest.php @@ -83,9 +83,48 @@ public function testGetRelation() { $modelName = self::MODEL_NAME; $entity = new $modelName(); /** @var MockModel Description */ + $mock = m::mock('LaravelRelation'); + $entity->setRelation($mock); + $result = $this->object->myRelation($entity); - $this->assertSame([], $result); + $this->assertSame($mock, $result); + $this->assertSame(1, $entity->getCallCount('myRelation')); + } + + public function testAddTo() { + $modelName = self::MODEL_NAME; + $entity = new $modelName(); /** @var MockModel Description */ + + $obj = new \stdClass(); + $obj->name = rand(1, 10000); + + $mock = m::mock('LaravelRelation'); + $mock->shouldReceive('save')->once()->with($obj)->andReturn($obj); + $entity->setRelation($mock); + + //add the object to the relation 'myRelation': + $result = $this->object->myRelation($entity, $obj); + + $this->assertSame(1, $entity->getCallCount('myRelation')); + $this->assertSame($mock, $result); + } + + public function testAddToList() { + $modelName = self::MODEL_NAME; + $entity = new $modelName(); /** @var MockModel Description */ + + $objs[] = new \stdClass(); + $objs[] = new \stdClass(); + + $mock = m::mock('LaravelRelation'); + $mock->shouldReceive('saveMany')->once()->with($objs)->andReturn($objs); + $entity->setRelation($mock); + + //add the object to the relation 'myRelation': + $result = $this->object->myRelation($entity, $objs); + $this->assertSame(1, $entity->getCallCount('myRelation')); + $this->assertSame($mock, $result); } public function testCanWeMockIt() { diff --git a/tests/DoctorBeat/EloquentRepository/MockModel.php b/tests/DoctorBeat/EloquentRepository/MockModel.php index 7074a48..9652c96 100644 --- a/tests/DoctorBeat/EloquentRepository/MockModel.php +++ b/tests/DoctorBeat/EloquentRepository/MockModel.php @@ -15,6 +15,12 @@ class MockModel { 'myRelation' => 0, ); + protected $relation; + + function setRelation($relation) { + $this->relation = $relation; + } + public static function all() { $list = array(); @@ -66,6 +72,6 @@ public function getAttribute($key) { public function myRelation() { $this->callCount['myRelation']++; - return []; + return $this->relation; } }