Skip to content

Commit a5da934

Browse files
mano-lisweaverryan
authored andcommitted
Fix(Doctrine Repository template)/Avoid potential double call in save method
1 parent 695f4df commit a5da934

File tree

3 files changed

+18
-12
lines changed

3 files changed

+18
-12
lines changed

src/Resources/skeleton/doctrine/Repository.tpl.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,21 @@ public function __construct(ManagerRegistry $registry)
2121

2222
public function save(<?= $entity_class_name ?> $entity, bool $flush = false): void
2323
{
24-
$this->getEntityManager()->persist($entity);
24+
$entityManager = $this->getEntityManager();
25+
$entityManager->persist($entity);
2526

2627
if ($flush) {
27-
$this->getEntityManager()->flush();
28+
$entityManager->flush();
2829
}
2930
}
3031

3132
public function remove(<?= $entity_class_name ?> $entity, bool $flush = false): void
3233
{
33-
$this->getEntityManager()->remove($entity);
34+
$entityManager = $this->getEntityManager();
35+
$entityManager->remove($entity);
3436

3537
if ($flush) {
36-
$this->getEntityManager()->flush();
38+
$entityManager->flush();
3739
}
3840
}
3941
<?php if ($include_example_comments): // When adding a new method without existing default comments, the blank line is automatically added.?>

tests/Doctrine/fixtures/expected_xml/src/Repository/UserRepository.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@ public function __construct(ManagerRegistry $registry)
2323

2424
public function save(UserXml $entity, bool $flush = false): void
2525
{
26-
$this->getEntityManager()->persist($entity);
26+
$entityManager = $this->getEntityManager();
27+
$entityManager->persist($entity);
2728

2829
if ($flush) {
29-
$this->getEntityManager()->flush();
30+
$entityManager->flush();
3031
}
3132
}
3233

3334
public function remove(UserXml $entity, bool $flush = false): void
3435
{
35-
$this->getEntityManager()->remove($entity);
36+
$entityManager = $this->getEntityManager();
37+
$entityManager->remove($entity);
3638

3739
if ($flush) {
38-
$this->getEntityManager()->flush();
40+
$entityManager->flush();
3941
}
4042
}
4143

tests/Doctrine/fixtures/expected_xml/src/Repository/XOtherRepository.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,21 @@ public function __construct(ManagerRegistry $registry)
2323

2424
public function save(XOther $entity, bool $flush = false): void
2525
{
26-
$this->getEntityManager()->persist($entity);
26+
$entityManager = $this->getEntityManager();
27+
$entityManager->persist($entity);
2728

2829
if ($flush) {
29-
$this->getEntityManager()->flush();
30+
$entityManager->flush();
3031
}
3132
}
3233

3334
public function remove(XOther $entity, bool $flush = false): void
3435
{
35-
$this->getEntityManager()->remove($entity);
36+
$entityManager = $this->getEntityManager();
37+
$entityManager->remove($entity);
3638

3739
if ($flush) {
38-
$this->getEntityManager()->flush();
40+
$entityManager->flush();
3941
}
4042
}
4143

0 commit comments

Comments
 (0)