Remove comment bloat from Eloquent hydrator.
This commit is contained in:
parent
bc8c0f2cc7
commit
34605ad49d
|
@ -12,14 +12,10 @@ class Hydrator {
|
||||||
*/
|
*/
|
||||||
public static function hydrate($eloquent)
|
public static function hydrate($eloquent)
|
||||||
{
|
{
|
||||||
// -----------------------------------------------------
|
|
||||||
// Load the base / parent models from the query results.
|
// Load the base / parent models from the query results.
|
||||||
// -----------------------------------------------------
|
|
||||||
$results = static::base(get_class($eloquent), $eloquent->query->get());
|
$results = static::base(get_class($eloquent), $eloquent->query->get());
|
||||||
|
|
||||||
// -----------------------------------------------------
|
|
||||||
// Load all of the eager relationships.
|
// Load all of the eager relationships.
|
||||||
// -----------------------------------------------------
|
|
||||||
if (count($results) > 0)
|
if (count($results) > 0)
|
||||||
{
|
{
|
||||||
foreach ($eloquent->includes as $include)
|
foreach ($eloquent->includes as $include)
|
||||||
|
@ -54,11 +50,8 @@ private static function base($class, $results)
|
||||||
$model->attributes = (array) $result;
|
$model->attributes = (array) $result;
|
||||||
$model->exists = true;
|
$model->exists = true;
|
||||||
|
|
||||||
// -----------------------------------------------------
|
// The results are keyed by the ID on the record. This will allow us to conveniently
|
||||||
// The results are keyed by the ID on the record. This
|
// match them to child models during eager loading.
|
||||||
// will allow us to conveniently match them to child
|
|
||||||
// models during eager loading.
|
|
||||||
// -----------------------------------------------------
|
|
||||||
$models[$model->id] = $model;
|
$models[$model->id] = $model;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,39 +68,27 @@ private static function base($class, $results)
|
||||||
*/
|
*/
|
||||||
private static function eagerly($eloquent, &$parents, $include)
|
private static function eagerly($eloquent, &$parents, $include)
|
||||||
{
|
{
|
||||||
// -----------------------------------------------------
|
|
||||||
// Get the relationship Eloquent model.
|
// Get the relationship Eloquent model.
|
||||||
//
|
//
|
||||||
// We temporarily spoof the belongs_to key to allow the
|
// We temporarily spoof the belongs_to key to allow the query to be fetched without
|
||||||
// query to be fetched without any problems, since the
|
// any problems, since the belongs_to method actually gets the attribute.
|
||||||
// belongs_to method actually gets the attribute.
|
|
||||||
// -----------------------------------------------------
|
|
||||||
$eloquent->attributes[$spoof = $include.'_id'] = 0;
|
$eloquent->attributes[$spoof = $include.'_id'] = 0;
|
||||||
|
|
||||||
$relationship = $eloquent->$include();
|
$relationship = $eloquent->$include();
|
||||||
|
|
||||||
unset($eloquent->attributes[$spoof]);
|
unset($eloquent->attributes[$spoof]);
|
||||||
|
|
||||||
// -----------------------------------------------------
|
// Reset the WHERE clause and bindings on the query. We'll add our own WHERE clause soon.
|
||||||
// Reset the WHERE clause and bindings on the query.
|
|
||||||
// We'll add our own WHERE clause soon.
|
|
||||||
// -----------------------------------------------------
|
|
||||||
$relationship->query->where = 'WHERE 1 = 1';
|
$relationship->query->where = 'WHERE 1 = 1';
|
||||||
$relationship->query->bindings = array();
|
$relationship->query->bindings = array();
|
||||||
|
|
||||||
// -----------------------------------------------------
|
// Initialize the relationship attribute on the parents. As expected, "many" relationships
|
||||||
// Initialize the relationship attribute on the parents.
|
// are initialized to an array and "one" relationships are initialized to null.
|
||||||
// As expected, "many" relationships are initialized to
|
|
||||||
// an array and "one" relationships to null.
|
|
||||||
// -----------------------------------------------------
|
|
||||||
foreach ($parents as &$parent)
|
foreach ($parents as &$parent)
|
||||||
{
|
{
|
||||||
$parent->ignore[$include] = (strpos($eloquent->relating, 'has_many') === 0) ? array() : null;
|
$parent->ignore[$include] = (strpos($eloquent->relating, 'has_many') === 0) ? array() : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------
|
|
||||||
// Eagerly load the relationships. Phew, almost there!
|
|
||||||
// -----------------------------------------------------
|
|
||||||
if ($eloquent->relating == 'has_one')
|
if ($eloquent->relating == 'has_one')
|
||||||
{
|
{
|
||||||
static::eagerly_load_one($relationship, $parents, $eloquent->relating_key, $include);
|
static::eagerly_load_one($relationship, $parents, $eloquent->relating_key, $include);
|
||||||
|
@ -138,14 +119,6 @@ private static function eagerly($eloquent, &$parents, $include)
|
||||||
*/
|
*/
|
||||||
private static function eagerly_load_one($relationship, &$parents, $relating_key, $include)
|
private static function eagerly_load_one($relationship, &$parents, $relating_key, $include)
|
||||||
{
|
{
|
||||||
// -----------------------------------------------------
|
|
||||||
// Get the all of the related models by the parent IDs.
|
|
||||||
//
|
|
||||||
// Remember, the parent results are keyed by ID. So, we
|
|
||||||
// can simply pass the keys of the array into the query.
|
|
||||||
//
|
|
||||||
// After getting the models, we'll match by ID.
|
|
||||||
// -----------------------------------------------------
|
|
||||||
foreach ($relationship->where_in($relating_key, array_keys($parents))->get() as $key => $child)
|
foreach ($relationship->where_in($relating_key, array_keys($parents))->get() as $key => $child)
|
||||||
{
|
{
|
||||||
$parents[$child->$relating_key]->ignore[$include] = $child;
|
$parents[$child->$relating_key]->ignore[$include] = $child;
|
||||||
|
@ -181,11 +154,8 @@ private static function eagerly_load_many($relationship, &$parents, $relating_ke
|
||||||
*/
|
*/
|
||||||
private static function eagerly_load_belonging($relationship, &$parents, $relating_key, $include)
|
private static function eagerly_load_belonging($relationship, &$parents, $relating_key, $include)
|
||||||
{
|
{
|
||||||
// -----------------------------------------------------
|
// Gather the keys from the parent models. Since the foreign key is on the parent model
|
||||||
// Gather the keys from the parent models. Since the
|
// for this type of relationship, we have to gather them individually.
|
||||||
// foreign key is on the parent model for this type of
|
|
||||||
// relationship, we have to gather them individually.
|
|
||||||
// -----------------------------------------------------
|
|
||||||
$keys = array();
|
$keys = array();
|
||||||
|
|
||||||
foreach ($parents as &$parent)
|
foreach ($parents as &$parent)
|
||||||
|
@ -193,14 +163,8 @@ private static function eagerly_load_belonging($relationship, &$parents, $relati
|
||||||
$keys[] = $parent->$relating_key;
|
$keys[] = $parent->$relating_key;
|
||||||
}
|
}
|
||||||
|
|
||||||
// -----------------------------------------------------
|
|
||||||
// Get the related models.
|
|
||||||
// -----------------------------------------------------
|
|
||||||
$children = $relationship->where_in('id', array_unique($keys))->get();
|
$children = $relationship->where_in('id', array_unique($keys))->get();
|
||||||
|
|
||||||
// -----------------------------------------------------
|
|
||||||
// Match the child models with their parent by ID.
|
|
||||||
// -----------------------------------------------------
|
|
||||||
foreach ($parents as &$parent)
|
foreach ($parents as &$parent)
|
||||||
{
|
{
|
||||||
if (array_key_exists($parent->$relating_key, $children))
|
if (array_key_exists($parent->$relating_key, $children))
|
||||||
|
@ -225,21 +189,16 @@ private static function eagerly_load_many_to_many($relationship, &$parents, $rel
|
||||||
{
|
{
|
||||||
$relationship->query->select = null;
|
$relationship->query->select = null;
|
||||||
|
|
||||||
// -----------------------------------------------------
|
|
||||||
// Retrieve the raw results as stdClasses.
|
// Retrieve the raw results as stdClasses.
|
||||||
//
|
//
|
||||||
// We also add the foreign key to the select which will allow us
|
// We also add the foreign key to the select which will allow us to match the
|
||||||
// to match the models back to their parents.
|
// models back to their parents.
|
||||||
// -----------------------------------------------------
|
|
||||||
$children = $relationship->query
|
$children = $relationship->query
|
||||||
->where_in($relating_table.'.'.$relating_key, array_keys($parents))
|
->where_in($relating_table.'.'.$relating_key, array_keys($parents))
|
||||||
->get(Eloquent::table(get_class($relationship)).'.*', $relating_table.'.'.$relating_key);
|
->get(Eloquent::table(get_class($relationship)).'.*', $relating_table.'.'.$relating_key);
|
||||||
|
|
||||||
$class = get_class($relationship);
|
$class = get_class($relationship);
|
||||||
|
|
||||||
// -----------------------------------------------------
|
|
||||||
// Create the related models.
|
|
||||||
// -----------------------------------------------------
|
|
||||||
foreach ($children as $child)
|
foreach ($children as $child)
|
||||||
{
|
{
|
||||||
$related = new $class;
|
$related = new $class;
|
||||||
|
@ -247,15 +206,10 @@ private static function eagerly_load_many_to_many($relationship, &$parents, $rel
|
||||||
$related->attributes = (array) $child;
|
$related->attributes = (array) $child;
|
||||||
$related->exists = true;
|
$related->exists = true;
|
||||||
|
|
||||||
// -----------------------------------------------------
|
// Remove the foreign key from the attributes since it was added to the query
|
||||||
// Remove the foreign key from the attributes since it
|
// to help us match the models.
|
||||||
// was added to the query to help us match the models.
|
|
||||||
// -----------------------------------------------------
|
|
||||||
unset($related->attributes[$relating_key]);
|
unset($related->attributes[$relating_key]);
|
||||||
|
|
||||||
// -----------------------------------------------------
|
|
||||||
// Match the child model its parent by ID.
|
|
||||||
// -----------------------------------------------------
|
|
||||||
$parents[$child->$relating_key]->ignore[$include][$child->id] = $related;
|
$parents[$child->$relating_key]->ignore[$include][$child->id] = $related;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue