Commit d78e126c authored by Jens Segers's avatar Jens Segers

Allow execution of collection methods on embedded relations

parent f6e43f54
......@@ -34,6 +34,7 @@
</testsuite>
<testsuite name="relations">
<directory>tests/RelationsTest.php</directory>
<directory>tests/EmbeddedRelationsTest.php</directory>
</testsuite>
<testsuite name="mysqlrelations">
<directory>tests/RelationsTest.php</directory>
......
......@@ -19,36 +19,17 @@ class EmbedsMany extends EmbedsOneOrMany {
}
/**
* Find an embedded model by its primary key.
* Simulate order by method.
*
* @param mixed $id
* @return \Illuminate\Database\Eloquent\Collection
* @param string $column
* @param string $direction
* @return Illuminate\Database\Eloquent\Collection
*/
public function find($id)
public function orderBy($column, $direction = 'asc')
{
if ($id instanceof Model) $id = $id->getKey();
$primaryKey = $this->related->getKeyName();
$descending = strtolower($direction) == 'desc';
// Traverse all embedded records and find the first record
// that matches the given primary key.
$record = array_first($this->getEmbedded(), function($itemKey, $record) use ($primaryKey, $id)
{
return $record[$primaryKey] == $id;
});
return $record ? $this->toModel($record) : null;
}
/**
* Check if a model is already embedded.
*
* @param mixed $key
* @return bool
*/
public function contains($key)
{
return ! is_null($this->find($key));
return $this->getResults()->sortBy($column, SORT_REGULAR, $descending);
}
/**
......@@ -314,4 +295,22 @@ class EmbedsMany extends EmbedsOneOrMany {
return parent::setEmbedded(array_values($models));
}
/**
* Handle dynamic method calls to the relationship.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
// Collection methods
if (method_exists('Illuminate\Database\Eloquent\Collection', $method))
{
return call_user_func_array(array($this->getResults(), $method), $parameters);
}
return parent::__call($method, $parameters);
}
}
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment