Skip to content

Commit 1c07ca1

Browse files
Test and refactor EmbedsMany::createMany
1 parent 42a83a2 commit 1c07ca1

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/Jenssegers/Mongodb/Relations/EmbedsMany.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,7 @@ public function create(array $attributes)
265265
*/
266266
public function createMany(array $records)
267267
{
268-
$instances = array();
269-
270-
foreach ($records as $record)
271-
{
272-
$instances[] = $this->create($record);
273-
}
268+
$instances = array_map(array($this, 'create'), $records);
274269

275270
return $instances;
276271
}

tests/RelationsTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,18 @@ public function testEmbedsManyCreate()
355355
$this->assertInstanceOf('MongoID', $address->_id);
356356
}
357357

358+
public function testEmbedsManyCreateMany()
359+
{
360+
$user = User::create(array());
361+
list($bruxelles, $paris) = $user->addresses()->createMany(array(array('city' => 'Bruxelles'), array('city' => 'Paris')));
362+
$this->assertInstanceOf('Address', $bruxelles);
363+
$this->assertEquals('Bruxelles', $bruxelles->city);
364+
$this->assertEquals(array('Bruxelles', 'Paris'), $user->addresses->lists('city'));
365+
366+
$freshUser = User::find($user->id);
367+
$this->assertEquals(array('Bruxelles', 'Paris'), $freshUser->addresses->lists('city'));
368+
}
369+
358370
public function testEmbedsManyDestroy()
359371
{
360372
$user = User::create(array('name' => 'John Doe'));

0 commit comments

Comments
 (0)