2012-03-16 08:37:31 -05:00
|
|
|
<?php namespace Laravel\Database\Eloquent\Relationships;
|
|
|
|
|
|
|
|
|
|
class Has_Many extends Has_One_Or_Many {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the properly hydrated results for the relationship.
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function results()
|
|
|
|
|
{
|
|
|
|
|
return parent::get();
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-03 10:48:01 -05:00
|
|
|
/**
|
|
|
|
|
* Sync the association table with an array of models.
|
|
|
|
|
*
|
2012-04-03 10:55:04 -05:00
|
|
|
* @param mixed $models
|
2012-04-03 10:48:01 -05:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2012-04-03 10:55:04 -05:00
|
|
|
public function save($models)
|
2012-04-03 10:48:01 -05:00
|
|
|
{
|
2012-04-03 10:56:55 -05:00
|
|
|
// If the given "models" are not an array, we'll force them into an array so
|
|
|
|
|
// we can conveniently loop through them and insert all of them into the
|
|
|
|
|
// related database table assigned to the associated model instance.
|
2012-04-03 10:55:04 -05:00
|
|
|
if ( ! is_array($models)) $models = array($models);
|
|
|
|
|
|
2012-04-03 10:48:01 -05:00
|
|
|
$current = $this->table->lists($this->model->key());
|
|
|
|
|
|
|
|
|
|
foreach ($models as $attributes)
|
|
|
|
|
{
|
2012-04-03 11:00:45 -05:00
|
|
|
$class = get_class($this->model);
|
|
|
|
|
|
2012-04-03 10:55:04 -05:00
|
|
|
// If the "attributes" are actually an array of the related model we'll
|
|
|
|
|
// just use the existing instance instead of creating a fresh model
|
|
|
|
|
// instance for the attributes. This allows for validation.
|
2012-04-03 11:00:45 -05:00
|
|
|
if ($attributes instanceof $class)
|
2012-04-03 10:55:04 -05:00
|
|
|
{
|
|
|
|
|
$model = $attributes;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
$model = $this->fresh_model($attributes);
|
|
|
|
|
}
|
2012-04-03 10:48:01 -05:00
|
|
|
|
2012-04-10 11:26:03 -05:00
|
|
|
// We'll need to associate the model with its parent, so we'll set the
|
|
|
|
|
// foreign key on the model to the key of the parent model, making
|
|
|
|
|
// sure that the two models are associated in the database.
|
|
|
|
|
$foreign = $this->foreign_key();
|
|
|
|
|
|
|
|
|
|
$model->$foreign = $this->base->get_key();
|
|
|
|
|
|
2012-04-03 10:48:01 -05:00
|
|
|
$id = $model->get_key();
|
|
|
|
|
|
|
|
|
|
$model->exists = ( ! is_null($id) and in_array($id, $current));
|
|
|
|
|
|
|
|
|
|
// Before saving we'll force the entire model to be "dirty" so all of
|
|
|
|
|
// the attributes are saved. It shouldn't affect the updates as
|
|
|
|
|
// saving all the attributes shouldn't hurt anything.
|
|
|
|
|
$model->original = array();
|
|
|
|
|
|
|
|
|
|
$model->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-16 08:37:31 -05:00
|
|
|
/**
|
|
|
|
|
* 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] = array();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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-05 10:35:48 -03:00
|
|
|
{
|
2012-10-07 14:04:29 -05:00
|
|
|
$parent->relationships[$relationship] = $dictionary[$key];
|
2012-10-05 10:35:48 -03:00
|
|
|
}
|
2012-03-16 08:37:31 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|