Skip to content

Commit 13d51d3

Browse files
Adding help notes for the EntityManager::clear() method
1 parent d60c22e commit 13d51d3

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

tests/AppBundle/Controller/BlogControllerTest.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,18 @@ public function testNewComment()
8585

8686
$this->assertSame(Response::HTTP_FOUND, $client->getResponse()->getStatusCode());
8787

88+
// in order to improve performance, Doctrine doesn't make database calls to
89+
// get the objects already present in memory. This means that this test will fail
90+
// because new comments are added at the end of the list of comments and the
91+
// "@ORM\OrderBy()" defined in the Post entity won't be respected. The solution
92+
// is to call the clear() method on the Entity Manager, which removes the in-memory
93+
// changes and forces loading objects from the database again.
8894
$client->getContainer()->get('doctrine')->getManager()->clear();
8995

90-
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
9196
// The first one is the most recent comment because of the automatic sorting
92-
// defined in the comments association of the Post entity
97+
// defined in the comments association of the Post entity. This test can only
98+
// work when using the clear() method as explained in the above comment.
99+
$post = $client->getContainer()->get('doctrine')->getRepository(Post::class)->find(1);
93100
$comment = $post->getComments()->first();
94101

95102
$this->assertSame($commentsCount + 1, $post->getComments()->count());

0 commit comments

Comments
 (0)