Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,80 @@ public function saveMany(array $models, array $joinings = array())
return $models;
}

/**
* Find a related model by its primary key or return new instance of the related model.
*
* @param mixed $id
* @param array $columns
* @return \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model
*/
public function findOrNew($id, $columns = array('*'))
{
if(is_null($instance = $this->find($id, $columns)))
{
$instance = $this->related->newInstance();
}

return $instance;
}

/**
* Get the first related record matching the attributes or create it.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrCreate(array $attributes, array $joining = array(), $touch = true)
{
if (is_null($instance = $this->where($attributes)->first()))
{
$instance = $this->create($attributes, $joining, $touch);
}

return $instance;
}

/**
* Get the first related model record matching the attributes or instantiate it.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrNew(array $attributes)
{
if (is_null($instance = $this->where($attributes)->first()))
{
$instance = $this->related->newInstance();
}

return $instance;
}

/**
* Create or update a related record matching the attributes, and fill it with values.
*
* @param array $attributes
* @param array $values
* @return \Illuminate\Database\Eloquent\Model
*/
public function updateOrCreate(array $attributes, array $values = array(), array $joining = array(), $touch = true)
{
if (is_null($instance = $this->where($attributes)->first()))
{
$instance = $this->related->newInstance();

$new = true;
}

$instance->fill($values);

$instance->save(array('touch' => false));

if ($new) $this->attach($instance->getKey(), $joining, $touch);

return $instance;
}

/**
* Create a new instance of the related model.
*
Expand Down
76 changes: 76 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,82 @@ public function saveMany(array $models)
return $models;
}

/**
* Find a model by its primary key or return new instance of the related model.
*
* @param mixed $id
* @param array $columns
* @return \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model
*/
public function findOrNew($id, $columns = array('*'))
{
if(is_null($instance = $this->find($id, $columns)))
{
$instance = $this->related->newInstance();
}

if( ! $instance instanceof Collection)
{
$instance->setAttribute($this->getPlainForeignKey(), $this->getParentKey());
}

return $instance;
}

/**
* Get the first related record matching the attributes or create it.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrCreate(array $attributes)
{
if (is_null($instance = $this->where($attributes)->first()))
{
$instance = $this->create($attributes);
}

return $instance;
}

/**
* Get the first related model record matching the attributes or instantiate it.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrNew(array $attributes)
{
if (is_null($instance = $this->where($attributes)->first()))
{
$instance = $this->related->newInstance();

$instance->setAttribute($this->getPlainForeignKey(), $this->getParentKey());
}

return $instance;
}

/**
* Create or update a related record matching the attributes, and fill it with values.
*
* @param array $attributes
* @param array $values
* @return \Illuminate\Database\Eloquent\Model
*/
public function updateOrCreate(array $attributes, array $values = array())
{
$instance = $this->firstOrNew($attributes);

$instance->fill($values);

$instance->setAttribute($this->getPlainForeignKey(), $this->getParentKey());

$instance->save();

return $instance;
}

/**
* Create a new instance of the related model.
*
Expand Down
85 changes: 85 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,91 @@ public function save(Model $model)
return parent::save($model);
}

/**
* Find a related model by its primary key or return new instance of the related model.
*
* @param mixed $id
* @param array $columns
* @return \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model
*/
public function findOrNew($id, $columns = array('*'))
{
if(is_null($instance = $this->find($id, $columns)))
{
$instance = $this->related->newInstance();
}

if( ! $instance instanceof Collection)
{
// When saving a polymorphic relationship, we need to set not only the foreign
// key, but also the foreign key type, which is typically the class name of
// the parent model. This makes the polymorphic item unique in the table.
$this->setForeignAttributesForCreate($instance);
}

return $instance;
}

/**
* Get the first related record matching the attributes or create it.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrCreate(array $attributes)
{
if (is_null($instance = $this->where($attributes)->first()))
{
$instance = $this->create($attributes);
}

return $instance;
}

/**
* Get the first related model record matching the attributes or instantiate it.
*
* @param array $attributes
* @return \Illuminate\Database\Eloquent\Model
*/
public function firstOrNew(array $attributes)
{
if (is_null($instance = $this->where($attributes)->first()))
{
$instance = $this->related->newInstance();

// When saving a polymorphic relationship, we need to set not only the foreign
// key, but also the foreign key type, which is typically the class name of
// the parent model. This makes the polymorphic item unique in the table.
$this->setForeignAttributesForCreate($instance);
}

return $instance;
}

/**
* Create or update a related record matching the attributes, and fill it with values.
*
* @param array $attributes
* @param array $values
* @return \Illuminate\Database\Eloquent\Model
*/
public function updateOrCreate(array $attributes, array $values = array())
{
$instance = $this->firstOrNew($attributes);

$instance->fill($values);

// When saving a polymorphic relationship, we need to set not only the foreign
// key, but also the foreign key type, which is typically the class name of
// the parent model. This makes the polymorphic item unique in the table.
$this->setForeignAttributesForCreate($instance);

$instance->save();

return $instance;
}

/**
* Create a new instance of the related model.
*
Expand Down