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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Embeddable;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\InverseJoinColumn;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\JoinTable;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\Table;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;
use Stringable;
class DDC3785Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();
Type::addType('ddc3785_asset_id', DDC3785AssetIdType::class);
$this->createSchemaForModels(
DDC3785Asset::class,
DDC3785AssetId::class,
DDC3785Attribute::class,
);
}
#[Group('DDC-3785')]
public function testOwningValueObjectIdIsCorrectlyTransformedWhenRemovingOrphanedChildEntities(): void
{
$id = new DDC3785AssetId('919609ba-57d9-4a13-be1d-d202521e858a');
$attributes = [
$attribute1 = new DDC3785Attribute('foo1', 'bar1'),
$attribute2 = new DDC3785Attribute('foo2', 'bar2'),
];
$this->_em->persist($asset = new DDC3785Asset($id, $attributes));
$this->_em->flush();
$asset->getAttributes()
->removeElement($attribute1);
$idToBeRemoved = $attribute1->id;
$this->_em->persist($asset);
$this->_em->flush();
self::assertNull($this->_em->find(DDC3785Attribute::class, $idToBeRemoved));
self::assertNotNull($this->_em->find(DDC3785Attribute::class, $attribute2->id));
}
}
#[Table(name: 'asset')]
#[Entity]
class DDC3785Asset
{
/** @phpstan-var Collection<int, DDC3785Attribute> */
#[JoinTable(name: 'asset_attributes')]
#[JoinColumn(name: 'asset_id', referencedColumnName: 'id')]
#[InverseJoinColumn(name: 'attribute_id', referencedColumnName: 'id')]
#[ManyToMany(targetEntity: 'DDC3785Attribute', cascade: ['persist'], orphanRemoval: true)]
private $attributes;
/** @phpstan-param list<DDC3785Attribute> $attributes */
public function __construct(
#[Id]
#[GeneratedValue(strategy: 'NONE')]
#[Column(type: 'ddc3785_asset_id')]
private DDC3785AssetId $id,
$attributes = [],
) {
$this->attributes = new ArrayCollection();
foreach ($attributes as $attribute) {
$this->attributes->add($attribute);
}
}
public function getId(): DDC3785AssetId
{
return $this->id;
}
/** @phpstan-return Collection<int, DDC3785Attribute> */
public function getAttributes()
{
return $this->attributes;
}
}
#[Table(name: 'attribute')]
#[Entity]
class DDC3785Attribute
{
/** @var int */
#[Id]
#[Column(type: 'integer')]
#[GeneratedValue]
public $id;
public function __construct(
#[Column(type: 'string', length: 255)]
private string $name,
#[Column(type: 'string', length: 255)]
private string $value,
) {
}
}
#[Embeddable]
class DDC3785AssetId implements Stringable
{
public function __construct(
#[Column(type: 'guid')]
private string $id,
) {
}
public function __toString(): string
{
return $this->id;
}
}
class DDC3785AssetIdType extends Type
{
/**
* {@inheritDoc}
*/
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getGuidTypeDeclarationSQL($column);
}
/**
* {@inheritDoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform): string
{
return (string) $value;
}
/**
* {@inheritDoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform): DDC3785AssetId
{
return new DDC3785AssetId($value);
}
public function getName(): string
{
return 'ddc3785_asset_id';
}
}
|