2012-03-16 08:37:31 -05:00
|
|
|
<?php namespace Laravel\Database\Eloquent\Relationships;
|
|
|
|
|
|
|
|
|
|
class Has_One extends Has_One_Or_Many {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the properly hydrated results for the relationship.
|
|
|
|
|
*
|
|
|
|
|
* @return Model
|
|
|
|
|
*/
|
|
|
|
|
public function results()
|
|
|
|
|
{
|
|
|
|
|
return parent::first();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Initialize a relationship on an array of parent models.
|
|
|
|
|
*
|
|
|
|
|
* @param array $parents
|
|
|
|
|
* @param string $relationship
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function initialize(&$parents, $relationship)
|
|
|
|
|
{
|
|
|
|
|
foreach ($parents as &$parent)
|
|
|
|
|
{
|
|
|
|
|
$parent->relationships[$relationship] = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Match eagerly loaded child models to their parent models.
|
|
|
|
|
*
|
2012-08-17 09:18:16 -05:00
|
|
|
* @param array $parents
|
|
|
|
|
* @param array $children
|
2012-03-16 08:37:31 -05:00
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function match($relationship, &$parents, $children)
|
|
|
|
|
{
|
|
|
|
|
$foreign = $this->foreign_key();
|
|
|
|
|
|
2012-10-07 14:04:29 -05:00
|
|
|
$dictionary = array();
|
|
|
|
|
|
2012-10-05 10:35:48 -03:00
|
|
|
foreach ($children as $child)
|
2012-03-16 08:37:31 -05:00
|
|
|
{
|
2012-10-07 14:04:29 -05:00
|
|
|
$dictionary[$child->$foreign] = $child;
|
2012-10-05 10:35:48 -03:00
|
|
|
}
|
2012-04-23 16:08:57 -05:00
|
|
|
|
2012-10-05 10:35:48 -03:00
|
|
|
foreach ($parents as $parent)
|
|
|
|
|
{
|
2012-10-07 14:04:29 -05:00
|
|
|
if (array_key_exists($key = $parent->get_key(), $dictionary))
|
2012-10-07 12:15:57 -03:00
|
|
|
{
|
2012-10-07 14:04:29 -05:00
|
|
|
$parent->relationships[$relationship] = $dictionary[$key];
|
2012-10-07 12:15:57 -03:00
|
|
|
}
|
2012-03-16 08:37:31 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|