From 0864ab8adac5c31d5ba97f0eb6792e5431a75b70 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Wed, 1 May 2013 20:30:45 +0200 Subject: [PATCH] [DDC-1998] Pass types to Connection#delete() to allow custom conversions to happen. --- .../ORM/Persisters/BasicEntityPersister.php | 28 ++++- .../ORM/Functional/Ticket/DDC1998Test.php | 102 ++++++++++++++++++ 2 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1998Test.php diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 8d2aa5236..96f144934 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -560,13 +560,35 @@ class BasicEntityPersister */ public function delete($entity) { + $class = $this->class; + $em = $this->em; + $identifier = $this->em->getUnitOfWork()->getEntityIdentifier($entity); - $tableName = $this->quoteStrategy->getTableName($this->class, $this->platform); - $idColumns = $this->quoteStrategy->getIdentifierColumnNames($this->class, $this->platform); + $tableName = $this->quoteStrategy->getTableName($class, $this->platform); + $idColumns = $this->quoteStrategy->getIdentifierColumnNames($class, $this->platform); $id = array_combine($idColumns, $identifier); + $types = array_map(function ($identifier) use ($class, $em) { + if (isset($class->fieldMappings[$identifier])) { + return $class->fieldMappings[$identifier]['type']; + } + + $targetMapping = $em->getClassMetadata($class->associationMappings[$identifier]['targetEntity']); + + if (isset($targetMapping->fieldMappings[$targetMapping->identifier[0]])) { + return $targetMapping->fieldMappings[$targetMapping->identifier[0]]['type']; + } + + if (isset($targetMapping->associationMappings[$targetMapping->identifier[0]])) { + $types[] = $targetMapping->associationMappings[$targetMapping->identifier[0]]['type']; + } + + throw ORMException::unrecognizedField($targetMapping->identifier[0]); + + }, $class->identifier); + $this->deleteJoinTableRecords($identifier); - $this->conn->delete($tableName, $id); + $this->conn->delete($tableName, $id, $types); } /** diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1998Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1998Test.php new file mode 100644 index 000000000..95e0a76f5 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1998Test.php @@ -0,0 +1,102 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\\DDC1998Entity'), + )); + + $entity = new DDC1998Entity(); + $entity->id = new DDC1998Id("foo"); + + $this->_em->persist($entity); + $this->_em->flush(); + + $entity->num++; + + $this->_em->flush(); + + $this->_em->remove($entity); + $this->_em->flush(); + $this->_em->clear(); + + + $found = $this->_em->find(__NAMESPACE__ . '\\DDC1998Entity', $entity->id); + $this->assertNull($found); + + $found = $this->_em->find(__NAMESPACE__ . '\\DDC1998Entity', "foo"); + $this->assertNull($found); + + $this->assertEquals(0, count($this->_em->getRepository(__NAMESPACE__ . '\\DDC1998Entity')->findAll())); + } +} + +/** + * @Entity + */ +class DDC1998Entity +{ + /** + * @Id @Column(type="ddc1998") + */ + public $id; + + /** + * @Column(type="integer") + */ + public $num = 0; +} + +class DDC1998Type extends StringType +{ + const NAME = 'ddc1998'; + + /** + * {@inheritdoc} + */ + public function convertToDatabaseValue($value, AbstractPlatform $platform) + { + return (string)$value; + } + + public function convertToPhpValue($value, AbstractPlatform $platform) + { + return new DDC1998Id($value); + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return self::NAME; + } +} + +class DDC1998Id +{ + private $val; + + public function __construct($val) + { + $this->val = $val; + } + + public function __toString() + { + return $this->val; + } +}