1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;
#[Group('GH6499')]
class GH6499OneToManyRelationshipTest extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->createSchemaForModels(Application::class, Person::class, ApplicationPerson::class);
}
/**
* Test for the bug described in issue #6499.
*/
public function testIssue(): void
{
$person = new Person();
$this->_em->persist($person);
$application = new Application();
$this->_em->persist($application);
$applicationPerson = new ApplicationPerson($person, $application);
$this->_em->persist($applicationPerson);
$this->_em->flush();
$this->_em->clear();
$personFromDatabase = $this->_em->find(Person::class, $person->id);
$applicationFromDatabase = $this->_em->find(Application::class, $application->id);
self::assertEquals($personFromDatabase->id, $person->id, 'Issue #6499 will result in an integrity constraint violation before reaching this point.');
self::assertFalse($personFromDatabase->getApplicationPeople()->isEmpty());
self::assertEquals($applicationFromDatabase->id, $application->id, 'Issue #6499 will result in an integrity constraint violation before reaching this point.');
self::assertFalse($applicationFromDatabase->getApplicationPeople()->isEmpty());
}
}
#[ORM\Entity]
#[ORM\Table(name: 'GH6499OTM_application')]
class Application
{
/** @var int */
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
public $id;
/** @var Collection */
#[ORM\OneToMany(mappedBy: 'application', targetEntity: ApplicationPerson::class, orphanRemoval: true, cascade: ['persist'])]
#[ORM\JoinColumn(nullable: false)]
private $applicationPeople;
public function __construct()
{
$this->applicationPeople = new ArrayCollection();
}
public function getApplicationPeople(): Collection
{
return $this->applicationPeople;
}
}
#[ORM\Entity]
#[ORM\Table(name: 'GH6499OTM_person')]
class Person
{
/** @var int */
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
public $id;
/** @var Collection */
#[ORM\OneToMany(mappedBy: 'person', targetEntity: ApplicationPerson::class, orphanRemoval: true, cascade: ['persist'])]
#[ORM\JoinColumn(nullable: false)]
private $applicationPeople;
public function __construct()
{
$this->applicationPeople = new ArrayCollection();
}
public function getApplicationPeople(): Collection
{
return $this->applicationPeople;
}
}
#[ORM\Entity]
#[ORM\Table(name: 'GH6499OTM_application_person')]
class ApplicationPerson
{
/** @var int */
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
public $id;
/** @var Application */
#[ORM\ManyToOne(targetEntity: Application::class, inversedBy: 'applicationPeople', cascade: ['persist'])]
#[ORM\JoinColumn(nullable: false)]
public $application;
/** @var Person */
#[ORM\ManyToOne(targetEntity: Person::class, inversedBy: 'applicationPeople', cascade: ['persist'])]
#[ORM\JoinColumn(nullable: false)]
public $person;
public function __construct(Person $person, Application $application)
{
$this->person = $person;
$this->application = $application;
}
}
|