-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10915 from mpdude/post-events-later
Mitigate problems with `EntityManager::flush()` reentrance since 2.16.0 (Take 2)
- Loading branch information
Showing
4 changed files
with
225 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
tests/Doctrine/Tests/ORM/Functional/Ticket/GH10869Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\Tests\ORM\Functional\Ticket; | ||
|
||
use Doctrine\ORM\Event\PostPersistEventArgs; | ||
use Doctrine\ORM\Events; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Doctrine\Tests\OrmFunctionalTestCase; | ||
|
||
class GH10869Test extends OrmFunctionalTestCase | ||
{ | ||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->setUpEntitySchema([ | ||
GH10869Entity::class, | ||
]); | ||
} | ||
|
||
public function testPostPersistListenerUpdatingObjectFieldWhileOtherInsertPending(): void | ||
{ | ||
$entity1 = new GH10869Entity(); | ||
$this->_em->persist($entity1); | ||
|
||
$entity2 = new GH10869Entity(); | ||
$this->_em->persist($entity2); | ||
|
||
$this->_em->getEventManager()->addEventListener(Events::postPersist, new class { | ||
public function postPersist(PostPersistEventArgs $args): void | ||
{ | ||
$object = $args->getObject(); | ||
|
||
$objectManager = $args->getObjectManager(); | ||
$object->field = 'test ' . $object->id; | ||
$objectManager->flush(); | ||
} | ||
}); | ||
|
||
$this->_em->flush(); | ||
$this->_em->clear(); | ||
|
||
self::assertSame('test ' . $entity1->id, $entity1->field); | ||
self::assertSame('test ' . $entity2->id, $entity2->field); | ||
|
||
$entity1Reloaded = $this->_em->find(GH10869Entity::class, $entity1->id); | ||
self::assertSame($entity1->field, $entity1Reloaded->field); | ||
|
||
$entity2Reloaded = $this->_em->find(GH10869Entity::class, $entity2->id); | ||
self::assertSame($entity2->field, $entity2Reloaded->field); | ||
} | ||
} | ||
|
||
/** | ||
* @ORM\Entity | ||
*/ | ||
class GH10869Entity | ||
{ | ||
/** | ||
* @ORM\Id | ||
* @ORM\GeneratedValue | ||
* @ORM\Column(type="integer") | ||
* | ||
* @var ?int | ||
*/ | ||
public $id; | ||
|
||
/** | ||
* @ORM\Column(type="text", nullable=true) | ||
* | ||
* @var ?string | ||
*/ | ||
public $field; | ||
} |