Skip to content

Commit

Permalink
MorphToMany now inherits BelongsToMany features
Browse files Browse the repository at this point in the history
All simple value get/setters have been ported across to their relation objects
  • Loading branch information
daftspunk committed May 10, 2016
1 parent 58f1b1c commit 0d74c72
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 86 deletions.
64 changes: 2 additions & 62 deletions src/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1025,75 +1025,15 @@ protected function getRelationCaller()
*/
public function getRelationValue($relationName)
{
$relationType = $this->getRelationType($relationName);
$relationObj = $this->$relationName();
$value = null;

switch ($relationType) {
case 'belongsTo':
case 'hasOne':
case 'hasMany':
case 'morphTo':
case 'morphOne':
case 'attachOne':
case 'attachMany':
case 'belongsToMany':
$value = $relationObj->getSimpleValue();
break;

case 'morphToMany':
case 'morphedByMany':
$value = $relationObj->getRelatedIds();
break;
}

return $value;
return $this->$relationName()->getSimpleValue();
}

/**
* Sets a relation value directly from its attribute.
*/
protected function setRelationValue($relationName, $value)
{
$relationType = $this->getRelationType($relationName);
$relationObj = $this->$relationName();
$relationModel = $relationObj->getRelated();

switch ($relationType) {
case 'belongsTo':
case 'hasOne':
case 'hasMany':
case 'morphTo':
case 'morphOne':
case 'attachOne':
case 'attachMany':
case 'belongsToMany':
$relationObj->setSimpleValue($value);
break;

case 'morphToMany':
case 'morphedByMany':
// Nulling the relationship
if (!$value) {
if ($this->exists) $relationObj->detach();
break;
}

if (is_string($value)) $value = [$value];

// Do not sync until the model is saved
$this->bindEventOnce('model.afterSave', function() use ($relationObj, $value){
$relationObj->sync($value);
});

$relationCollection = $value instanceof CollectionBase
? $value
: $relationModel->whereIn($relationModel->getKeyName(), $value)->get();

// Associate
$this->setRelation($relationName, $relationCollection);
break;
}
$this->$relationName()->setSimpleValue($value);
}

//
Expand Down
164 changes: 140 additions & 24 deletions src/Database/Relations/MorphToMany.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,163 @@
<?php namespace October\Rain\Database\Relations;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany as MorphToManyBase;
use Illuminate\Database\Eloquent\Builder;

class MorphToMany extends MorphToManyBase
/**
* Morph to many
*
* This class is a carbon copy of Illuminate\Database\Eloquent\Relations\MorphToMany
* so the base October\Rain\Database\Relations\ BelongsToMany class can be inherited
*/
class MorphToMany extends BelongsToMany
{
/**
* @var string The "name" of the relationship.
* The type of the polymorphic relation.
*
* @var string
*/
protected $relationName;
protected $morphType;

/**
* Adds a model to this relationship type.
* The class name of the morph type constraint.
*
* @var string
*/
public function add(Model $model, $sessionKey = null, $pivotData = [])
protected $morphClass;

/**
* Indicates if we are connecting the inverse of the relation.
*
* This primarily affects the morphClass constraint.
*
* @var bool
*/
protected $inverse;

/**
* Create a new morph to many relationship instance.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Model $parent
* @param string $name
* @param string $table
* @param string $foreignKey
* @param string $otherKey
* @param string $relationName
* @param bool $inverse
* @return void
*/
public function __construct(Builder $query, Model $parent, $name, $table, $foreignKey, $otherKey, $relationName = null, $inverse = false)
{
$this->inverse = $inverse;
$this->morphType = $name.'_type';
$this->morphClass = $inverse ? $query->getModel()->getMorphClass() : $parent->getMorphClass();

parent::__construct($query, $parent, $table, $foreignKey, $otherKey, $relationName);
}

/**
* Set the where clause for the relation query.
*
* @return $this
*/
protected function setWhere()
{
parent::setWhere();

$this->query->where($this->table.'.'.$this->morphType, $this->morphClass);

return $this;
}

/**
* Add the constraints for a relationship count query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parent
* @return \Illuminate\Database\Eloquent\Builder
*/
public function getRelationCountQuery(Builder $query, Builder $parent)
{
$query = parent::getRelationCountQuery($query, $parent);

return $query->where($this->table.'.'.$this->morphType, $this->morphClass);
}

/**
* Set the constraints for an eager load of the relation.
*
* @param array $models
* @return void
*/
public function addEagerConstraints(array $models)
{
parent::addEagerConstraints($models);

$this->query->where($this->table.'.'.$this->morphType, $this->morphClass);
}

/**
* Create a new pivot attachment record.
*
* @param int $id
* @param bool $timed
* @return array
*/
protected function createAttachRecord($id, $timed)
{
$record = parent::createAttachRecord($id, $timed);

return Arr::add($record, $this->morphType, $this->morphClass);
}

/**
* Create a new query builder for the pivot table.
*
* @return \Illuminate\Database\Query\Builder
*/
protected function newPivotQuery()
{
if ($sessionKey === null) {
$this->attach($model->getKey(), $pivotData);
}
else {
$this->parent->bindDeferred($this->relationName, $model, $sessionKey);
}
$query = parent::newPivotQuery();

return $query->where($this->morphType, $this->morphClass);
}

/**
* Removes a model from this relationship type.
* Create a new pivot model instance.
*
* @param array $attributes
* @param bool $exists
* @return \Illuminate\Database\Eloquent\Relations\Pivot
*/
public function remove(Model $model, $sessionKey = null)
public function newPivot(array $attributes = [], $exists = false)
{
if ($sessionKey === null) {
$this->detach($model->getKey());
}
else {
$this->parent->unbindDeferred($this->relationName, $model, $sessionKey);
}
$pivot = new MorphPivot($this->parent, $attributes, $this->table, $exists);

$pivot->setPivotKeys($this->foreignKey, $this->otherKey)
->setMorphType($this->morphType)
->setMorphClass($this->morphClass);

return $pivot;
}

/**
* Returns the model query with deferred bindings added
* Get the foreign key "type" name.
*
* @return string
*/
public function withDeferred($sessionKey)
public function getMorphType()
{
// @todo See DeferOneOrMany trait
return $this->morphType;
}

}
/**
* Get the class name of the parent model.
*
* @return string
*/
public function getMorphClass()
{
return $this->morphClass;
}
}

0 comments on commit 0d74c72

Please sign in to comment.