-
-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug #278 Fix self-referencing entity associations (codedmonkey)
This PR was squashed before being merged into the 1.0-dev branch (closes #278). Discussion ---------- Fix self-referencing entity associations Fixes faulty generation of entities with self-referencing associations #166. It looks like changes for the owning entity were overwritten by the changes for the inversed entity. Commits ------- bed3b44 Fix self-referencing entity associations
- Loading branch information
Showing
7 changed files
with
167 additions
and
3 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
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
53 changes: 53 additions & 0 deletions
53
tests/fixtures/MakeEntitySelfReferencing/src/Entity/User.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,53 @@ | ||
<?php | ||
|
||
namespace App\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
|
||
/** | ||
* @ORM\Entity() | ||
*/ | ||
class User | ||
{ | ||
/** | ||
* @ORM\Id | ||
* @ORM\GeneratedValue | ||
* @ORM\Column(type="integer") | ||
*/ | ||
private $id; | ||
|
||
/** | ||
* @ORM\Column(type="string", length=255, nullable=true) | ||
*/ | ||
private $firstName; | ||
|
||
/** | ||
* @ORM\Column(type="datetime", nullable=true) | ||
*/ | ||
private $createdAt; | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getFirstName() | ||
{ | ||
return $this->firstName; | ||
} | ||
|
||
public function setFirstName(?string $firstName) | ||
{ | ||
$this->firstName = $firstName; | ||
} | ||
|
||
public function getCreatedAt(): ?\DateTimeInterface | ||
{ | ||
return $this->createdAt; | ||
} | ||
|
||
public function setCreatedAt(?\DateTimeInterface $createdAt) | ||
{ | ||
$this->createdAt = $createdAt; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
tests/fixtures/MakeEntitySelfReferencing/tests/GeneratedEntityTest.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,49 @@ | ||
<?php | ||
|
||
namespace App\Tests; | ||
|
||
use Doctrine\Common\Collections\ArrayCollection; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Doctrine\ORM\EntityManager; | ||
use App\Entity\User; | ||
|
||
class GeneratedEntityTest extends KernelTestCase | ||
{ | ||
public function testGeneratedEntity() | ||
{ | ||
self::bootKernel(); | ||
/** @var EntityManager $em */ | ||
$em = self::$kernel->getContainer() | ||
->get('doctrine') | ||
->getManager(); | ||
|
||
$em->createQuery('DELETE FROM App\\Entity\\User u')->execute(); | ||
|
||
$user = new User(); | ||
// check that the constructor was instantiated properly | ||
$this->assertInstanceOf(ArrayCollection::class, $user->getDependants()); | ||
// set existing field | ||
$user->setFirstName('Ryan'); | ||
$em->persist($user); | ||
|
||
$ward = new User(); | ||
$ward->setFirstName('Tim'); | ||
$ward->setGuardian($user); | ||
$em->persist($ward); | ||
|
||
// set via the inverse side | ||
$ward2 = new User(); | ||
$ward2->setFirstName('Fabien'); | ||
$user->addDependant($ward2); | ||
$em->persist($ward2); | ||
|
||
$em->flush(); | ||
$em->refresh($user); | ||
|
||
$actualUser = $em->getRepository(User::class) | ||
->findAll(); | ||
|
||
$this->assertCount(3, $actualUser); | ||
$this->assertCount(2, $actualUser[0]->getDependants()); | ||
} | ||
} |