Commit 78920c5d by Franz Liedke

Move automatic relationship parsing to Eloquent's query class.

parent 8c2a4fba
...@@ -257,7 +257,7 @@ abstract class Model { ...@@ -257,7 +257,7 @@ abstract class Model {
{ {
$includes = (array) $includes; $includes = (array) $includes;
$given_includes = array(); $this->includes = array();
foreach ($includes as $relationship => $constraints) foreach ($includes as $relationship => $constraints)
{ {
...@@ -269,27 +269,9 @@ abstract class Model { ...@@ -269,27 +269,9 @@ abstract class Model {
list($relationship, $constraints) = array($constraints, null); list($relationship, $constraints) = array($constraints, null);
} }
$given_includes[$relationship] = $constraints; $this->includes[$relationship] = $constraints;
} }
$relationships = array_keys($given_includes);
$implicits = array();
foreach ($relationships as $relationship)
{
$parts = explode('.', $relationship);
$prefix = '';
foreach ($parts as $part)
{
$implicits[$prefix.$part] = null;
$prefix .= $part.'.';
}
}
// Add all implicit includes to the explicit ones
$this->includes = $given_includes + $implicits;
return $this; return $this;
} }
......
...@@ -127,7 +127,7 @@ class Query { ...@@ -127,7 +127,7 @@ class Query {
if (count($results) > 0) if (count($results) > 0)
{ {
foreach ($this->model->includes as $relationship => $constraints) foreach ($this->model_includes() as $relationship => $constraints)
{ {
// If the relationship is nested, we will skip loading it here and let // If the relationship is nested, we will skip loading it here and let
// the load method parse and set the nested eager loads on the right // the load method parse and set the nested eager loads on the right
...@@ -196,7 +196,7 @@ class Query { ...@@ -196,7 +196,7 @@ class Query {
{ {
$nested = array(); $nested = array();
foreach ($this->model->includes as $include => $constraints) foreach ($this->model_includes() as $include => $constraints)
{ {
// To get the nested includes, we want to find any includes that begin // To get the nested includes, we want to find any includes that begin
// the relationship and a dot, then we will strip off the leading // the relationship and a dot, then we will strip off the leading
...@@ -211,6 +211,32 @@ class Query { ...@@ -211,6 +211,32 @@ class Query {
} }
/** /**
* Get the eagerly loaded relationships for the model.
*
* @return array
*/
protected function model_includes()
{
$relationships = array_keys($this->model->includes);
$implicits = array();
foreach ($relationships as $relationship)
{
$parts = explode('.', $relationship);
$prefix = '';
foreach ($parts as $part)
{
$implicits[$prefix.$part] = NULL;
$prefix .= $part.'.';
}
}
// Add all implicit includes to the explicit ones
return $this->model->includes + $implicits;
}
/**
* Get a fluent query builder for the model. * Get a fluent query builder for the model.
* *
* @return Query * @return Query
......
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