From 669c5356e65f0712d4f71f2cd8c6345838401ccb Mon Sep 17 00:00:00 2001 From: Joseph Silber Date: Sun, 5 Oct 2014 17:05:53 -0400 Subject: [PATCH] Set morphs directly --- .../Eloquent/Relations/MorphOneOrMany.php | 19 ++++++++----------- tests/Database/DatabaseEloquentMorphTest.php | 6 ++++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php b/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php index 9c6ae7e2d309..4a20351edce6 100755 --- a/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphOneOrMany.php @@ -101,14 +101,12 @@ public function save(Model $model) */ public function create(array $attributes) { - $foreign = $this->getForeignAttributesForCreate(); + $instance = $this->related->newInstance($attributes); // 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. - $attributes = array_merge($attributes, $foreign); - - $instance = $this->related->newInstance($attributes); + $this->setForeignAttributesForCreate($instance); $instance->save(); @@ -116,17 +114,16 @@ public function create(array $attributes) } /** - * Get the foreign ID and type for creating a related model. + * Set the foreign ID and type for creating a related model. * - * @return array + * @param \Illuminate\Database\Eloquent\Model $model + * @return void */ - protected function getForeignAttributesForCreate() + protected function setForeignAttributesForCreate(Model $model) { - $foreign = array($this->getPlainForeignKey() => $this->getParentKey()); - - $foreign[last(explode('.', $this->morphType))] = $this->morphClass; + $model->{$this->getPlainForeignKey()} = $this->getParentKey(); - return $foreign; + $model->{last(explode('.', $this->morphType))} = $this->morphClass; } /** diff --git a/tests/Database/DatabaseEloquentMorphTest.php b/tests/Database/DatabaseEloquentMorphTest.php index 93d64a4678b1..586282cba662 100755 --- a/tests/Database/DatabaseEloquentMorphTest.php +++ b/tests/Database/DatabaseEloquentMorphTest.php @@ -60,8 +60,10 @@ public function testCreateFunctionOnMorph() { // Doesn't matter which relation type we use since they share the code... $relation = $this->getOneRelation(); - $created = m::mock('stdClass'); - $relation->getRelated()->shouldReceive('newInstance')->once()->with(array('name' => 'taylor', 'morph_id' => 1, 'morph_type' => get_class($relation->getParent())))->andReturn($created); + $created = m::mock('Illuminate\Database\Eloquent\Model'); + $created->shouldReceive('setAttribute')->once()->with('morph_id', 1); + $created->shouldReceive('setAttribute')->once()->with('morph_type', get_class($relation->getParent())); + $relation->getRelated()->shouldReceive('newInstance')->once()->with(array('name' => 'taylor'))->andReturn($created); $created->shouldReceive('save')->once()->andReturn(true); $this->assertEquals($created, $relation->create(array('name' => 'taylor')));