From 6fa20062f330e3df951f8350a11a29cedcd96bc1 Mon Sep 17 00:00:00 2001 From: David Adams Date: Tue, 23 Jun 2015 16:26:49 +0000 Subject: [PATCH] allow ManyToManyPersister to handle identification types --- .../Collection/ManyToManyPersister.php | 9 +- .../ORM/Functional/Ticket/DDC3785Test.php | 163 ++++++++++++++++++ 2 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3785Test.php diff --git a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php index bf9f14e688..63d996db5a 100644 --- a/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php +++ b/lib/Doctrine/ORM/Persisters/Collection/ManyToManyPersister.php @@ -48,7 +48,14 @@ public function delete(PersistentCollection $collection) return; // ignore inverse side } - $this->conn->executeUpdate($this->getDeleteSQL($collection), $this->getDeleteSQLParameters($collection)); + $types = array(); + $class = $this->em->getClassMetadata($mapping['sourceEntity']); + + foreach ($mapping['joinTable']['joinColumns'] as $joinColumn) { + $types[] = PersisterHelper::getTypeOfColumn($joinColumn['referencedColumnName'], $class, $this->em); + } + + $this->conn->executeUpdate($this->getDeleteSQL($collection), $this->getDeleteSQLParameters($collection), $types); } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3785Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3785Test.php new file mode 100644 index 0000000000..4940c8abc8 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC3785Test.php @@ -0,0 +1,163 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC3785_Asset'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC3785_AssetId'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC3785_Attribute') + )); + } catch(\Exception $e) { + } + } + + /** + * @group DDC-3785 + */ + public function testOwningValueObjectIdIsCorrectlyTransformedWhenRemovingOrphanedChildEntities() + { + $id = new DDC3785_AssetId("919609ba-57d9-4a13-be1d-d202521e858a"); + $attributes = array( + $attribute1 = new DDC3785_Attribute("foo1", "bar1"), + $attribute2 = new DDC3785_Attribute("foo2", "bar2") + ); + $this->_em->persist($asset = new DDC3785_Asset($id, $attributes)); + $this->_em->flush(); + + $asset->getAttributes()->removeElement($attribute1); + + $this->_em->persist($asset); + $this->_em->flush(); + } +} + +/** + * @Entity + * @Table(name="asset") + */ +class DDC3785_Asset +{ + /** + * @Id @GeneratedValue(strategy="NONE") @Column(type="ddc3785_asset_id") + */ + private $id; + + /** + * @ManyToMany(targetEntity="DDC3785_Attribute", cascade={"persist"}, orphanRemoval=true) + * @JoinTable(name="asset_attributes", + * joinColumns={@JoinColumn(name="asset_id", referencedColumnName="id")}, + * inverseJoinColumns={@JoinColumn(name="attribute_id", referencedColumnName="id")} + * ) + **/ + private $attributes; + + public function __construct(DDC3785_AssetId $id, $attributes = array()) + { + $this->id = $id; + $this->attributes = new ArrayCollection(); + + foreach ($attributes as $attribute) { + $this->attributes->add($attribute); + } + } + + public function getId() + { + return $this->id; + } + + public function getAttributes() + { + return $this->attributes; + } +} + +/** + * @Entity + * @Table(name="attribute") + */ +class DDC3785_Attribute +{ + /** + * @Id @Column(type="integer") + * @GeneratedValue + */ + private $id; + + /** @Column(type = "string") */ + private $name; + + /** @Column(type = "string") */ + private $value; + + public function __construct($name, $value) + { + $this->name = $name; + $this->value = $value; + } +} + +/** @Embeddable */ +class DDC3785_AssetId +{ + /** @Column(type = "guid") */ + private $id; + + public function __construct($id) + { + $this->id = $id; + } + + public function __toString() + { + return $this->id; + } +} + +class DDC3785_AssetIdType 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 DDC3785_AssetId($value); + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'ddc3785_asset_id'; + } +}