Skip to content

Commit

Permalink
Merge pull request #8800 from jderusse/fix-exception
Browse files Browse the repository at this point in the history
Fix exception not thrown by "getEntityIdentifier"
  • Loading branch information
greg0ire authored Jun 29, 2021
2 parents 233d9b0 + 796af72 commit 1f6bfe1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ public function executeInserts()
$paramIndex = 1;
$data = $insertData[$tableName] ?? [];

foreach ((array) $id as $idName => $idVal) {
foreach ($id as $idName => $idVal) {
$type = $this->columnTypes[$idName] ?? Type::STRING;

$stmt->bindValue($paramIndex++, $idVal, $type);
}

foreach ($data as $columnName => $value) {
if (! is_array($id) || ! isset($id[$columnName])) {
if (! isset($id[$columnName])) {
$stmt->bindValue($paramIndex++, $value, $this->columnTypes[$columnName]);
}
}
Expand Down
9 changes: 6 additions & 3 deletions lib/Doctrine/ORM/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -3056,12 +3056,15 @@ public function setOriginalEntityProperty($oid, $property, $value)
*
* @param object $entity
*
* @return mixed The identifier values.
* @return mixed[] The identifier values.
*/
public function getEntityIdentifier($entity)
{
return $this->entityIdentifiers[spl_object_hash($entity)]
?? EntityNotFoundException::noIdentifierFound(get_class($entity));
if (! isset($this->entityIdentifiers[spl_object_hash($entity)])) {
throw EntityNotFoundException::noIdentifierFound(get_class($entity));
}

return $this->entityIdentifiers[spl_object_hash($entity)];
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/Doctrine/Tests/ORM/UnitOfWorkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\EventManager;
use Doctrine\ORM\EntityNotFoundException;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\OptimisticLockException;
Expand Down Expand Up @@ -815,6 +816,12 @@ public function testCommitThrowOptimisticLockExceptionWhenConnectionCommitReturn
$this->expectException(OptimisticLockException::class);
$this->_unitOfWork->commit();
}

public function testItThrowsWhenLookingUpIdentifierForUnknownEntity(): void
{
$this->expectException(EntityNotFoundException::class);
$this->_unitOfWork->getEntityIdentifier(new stdClass());
}
}

/**
Expand Down

0 comments on commit 1f6bfe1

Please sign in to comment.