Skip to content

Make model relationship creates filtered by fillable array #2846

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
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
8 changes: 4 additions & 4 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,12 @@ public function create(array $attributes)
$this->getPlainForeignKey() => $this->getParentKey(),
);

// Here we will set the raw attributes to avoid hitting the "fill" method so
// that we do not have to worry about a mass accessor rules blocking sets
// on the models. Otherwise, some of these attributes will not get set.
// First fill the attributes with input data
// Then override them with foreign keys and data already filtered by fillable
$instance = $this->related->newInstance();

$instance->setRawAttributes(array_merge($attributes, $foreign));
$instance->fill($attributes);
$instance->setRawAttributes(array_merge($instance->getAttributes(), $foreign));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm missing something, shouldn't you be able to just call $instance->fill(array_merge($attributes, $foreign));?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The foreign key will rarely be fillable.


$instance->save();

Expand Down
20 changes: 15 additions & 5 deletions tests/Database/DatabaseEloquentHasManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ public function tearDown()
public function testCreateMethodProperlyCreatesNewModel()
{
$relation = $this->getRelation();
$created = $this->getMock('Illuminate\Database\Eloquent\Model', array('save', 'getKey', 'setRawAttributes'));
$created->expects($this->once())->method('save')->will($this->returnValue(true));
$created = new EloquentHasManyRelationStub;
$relation->getRelated()->shouldReceive('newInstance')->once()->andReturn($created);
$created->expects($this->once())->method('setRawAttributes')->with($this->equalTo(array('name' => 'taylor', 'foreign_key' => 1)));

$this->assertEquals($created, $relation->create(array('name' => 'taylor')));
$related = $relation->create(array('name' => 'taylor', 'foo' => 'bar'));
$this->assertEquals('taylor', $related->name);
$this->assertNull($related->foo);
$this->assertEquals(1, $related->foreign_key);
$this->assertTrue($related->exists);
}

public function testUpdateMethodUpdatesModelsWithTimestamps()
Expand Down Expand Up @@ -106,4 +107,13 @@ protected function getRelation()

class EloquentHasManyModelStub extends Illuminate\Database\Eloquent\Model {
public $foreign_key = 'foreign.value';
}

class EloquentHasManyRelationStub extends Illuminate\Database\Eloquent\Model {
protected $fillable = array('name');
public function save(array $options = array())
{
$this->exists = true;
return true;
}
}
20 changes: 15 additions & 5 deletions tests/Database/DatabaseEloquentHasOneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ public function testSaveMethodSetsForeignKeyOnModel()
public function testCreateMethodProperlyCreatesNewModel()
{
$relation = $this->getRelation();
$created = $this->getMock('Illuminate\Database\Eloquent\Model', array('save', 'getKey', 'setRawAttributes'));
$created->expects($this->once())->method('save')->will($this->returnValue(true));
$created = new EloquentHasOneRelationStub;
$relation->getRelated()->shouldReceive('newInstance')->once()->andReturn($created);
$created->expects($this->once())->method('setRawAttributes')->with($this->equalTo(array('name' => 'taylor', 'foreign_key' => 1)));

$this->assertEquals($created, $relation->create(array('name' => 'taylor')));
$related = $relation->create(array('name' => 'taylor', 'foo' => 'bar'));
$this->assertEquals('taylor', $related->name);
$this->assertNull($related->foo);
$this->assertEquals(1, $related->foreign_key);
$this->assertTrue($related->exists);
}


Expand Down Expand Up @@ -127,4 +128,13 @@ protected function getRelation()

class EloquentHasOneModelStub extends Illuminate\Database\Eloquent\Model {
public $foreign_key = 'foreign.value';
}

class EloquentHasOneRelationStub extends Illuminate\Database\Eloquent\Model {
protected $fillable = array('name');
public function save(array $options = array())
{
$this->exists = true;
return true;
}
}