Skip to content

Commit

Permalink
better support for relations
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronald committed Mar 31, 2015
1 parent ece8f56 commit dca3e48
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 5 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
10 changes: 8 additions & 2 deletions src/DoctorBeat/EloquentRepository/EloquentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
1 change: 0 additions & 1 deletion src/DoctorBeat/EloquentRepository/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ function touch($entity);
function all();
function find($id);
function getAttribute($entity, $key);
function addTo($relation, $entities);
function associate($relation, $entity);
}
41 changes: 40 additions & 1 deletion tests/DoctorBeat/EloquentRepository/EloquentRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 7 additions & 1 deletion tests/DoctorBeat/EloquentRepository/MockModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ class MockModel {
'myRelation' => 0,
);

protected $relation;

function setRelation($relation) {
$this->relation = $relation;
}

public static function all() {
$list = array();

Expand Down Expand Up @@ -66,6 +72,6 @@ public function getAttribute($key) {

public function myRelation() {
$this->callCount['myRelation']++;
return [];
return $this->relation;
}
}

0 comments on commit dca3e48

Please sign in to comment.