Skip to content

Commit

Permalink
Merge 7cd3c52 into 5463870
Browse files Browse the repository at this point in the history
  • Loading branch information
dakota committed Jun 1, 2020
2 parents 5463870 + 7cd3c52 commit 2333463
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/ORM/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -2180,6 +2180,14 @@ public function saveManyOrFail(iterable $entities, $options = []): iterable
*/
protected function _saveMany(iterable $entities, $options = []): iterable
{
$options = new ArrayObject(
(array)$options + [
'atomic' => true,
'checkRules' => true,
'_primary' => true,
]
);

/** @var bool[] $isNew */
$isNew = [];
$cleanup = function ($entities) use (&$isNew): void {
Expand Down Expand Up @@ -2217,7 +2225,13 @@ protected function _saveMany(iterable $entities, $options = []): iterable

throw new PersistenceFailedException($failed, ['saveMany']);
}


if ($this->_transactionCommitted($options['atomic'], $options['_primary'])) {
foreach ($entities as $entity) {
$this->dispatchEvent('Model.afterSaveCommit', compact('entity', 'options'));
}
}

return $entities;
}

Expand Down
12 changes: 11 additions & 1 deletion tests/TestCase/ORM/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2811,14 +2811,24 @@ public function testSaveManyArray()
new Entity(['name' => 'dakota']),
];

$table = $this->getTableLocator()->get('authors');
$timesCalled = 0;
$listener = function ($e, $entity, $options) use (&$timesCalled) {
$timesCalled++;
};
$table = $this->getTableLocator()
->get('authors');

$table->getEventManager()
->on('Model.afterSaveCommit', $listener);

$result = $table->saveMany($entities);

$this->assertSame($entities, $result);
$this->assertTrue(isset($result[0]->id));
foreach ($entities as $entity) {
$this->assertFalse($entity->isNew());
}
$this->assertSame(2, $timesCalled);
}

/**
Expand Down

0 comments on commit 2333463

Please sign in to comment.