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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
<?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\JoinColumn;
use Doctrine\ORM\Mapping\JoinTable;
use Doctrine\ORM\Mapping\ManyToMany;
use Doctrine\ORM\Mapping\Table;
use Doctrine\Tests\OrmFunctionalTestCase;
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));
}
}
/**
* @Entity
* @Table(name="asset")
*/
class DDC3785Asset
{
/**
* @var DDC3785AssetId
* @Id
* @GeneratedValue(strategy="NONE")
* @Column(type="ddc3785_asset_id")
*/
private $id;
/**
* @psalm-var Collection<int, DDC3785Attribute>
* @ManyToMany(targetEntity="DDC3785Attribute", cascade={"persist"}, orphanRemoval=true)
* @JoinTable(name="asset_attributes",
* joinColumns={@JoinColumn(name="asset_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="attribute_id", referencedColumnName="id")}
* )
*/
private $attributes;
/** @psalm-param list<DDC3785Attribute> $attributes */
public function __construct(DDC3785AssetId $id, $attributes = [])
{
$this->id = $id;
$this->attributes = new ArrayCollection();
foreach ($attributes as $attribute) {
$this->attributes->add($attribute);
}
}
public function getId(): DDC3785AssetId
{
return $this->id;
}
/** @psalm-return Collection<int, DDC3785Attribute> */
public function getAttributes()
{
return $this->attributes;
}
}
/**
* @Entity
* @Table(name="attribute")
*/
class DDC3785Attribute
{
/**
* @var int
* @Id
* @Column(type="integer")
* @GeneratedValue
*/
public $id;
/**
* @var string
* @Column(type="string", length=255)
*/
private $name;
/**
* @var string
* @Column(type="string", length=255)
*/
private $value;
public function __construct(string $name, string $value)
{
$this->name = $name;
$this->value = $value;
}
}
/** @Embeddable */
class DDC3785AssetId
{
/**
* @var string
* @Column(type = "guid")
*/
private $id;
public function __construct(string $id)
{
$this->id = $id;
}
public function __toString(): string
{
return $this->id;
}
}
class DDC3785AssetIdType extends Type
{
/**
* {@inheritdoc}
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return $platform->getGuidTypeDeclarationSQL($fieldDeclaration);
}
/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return (string) $value;
}
/**
* {@inheritdoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return new DDC3785AssetId($value);
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'ddc3785_asset_id';
}
}
|