1
0
mirror of synced 2025-01-18 22:41:43 +03:00

[DDC-1998] Pass types to Connection#delete() to allow custom conversions to happen.

This commit is contained in:
Benjamin Eberlei 2013-05-01 20:30:45 +02:00
parent 7d53cb2aeb
commit 0864ab8ada
2 changed files with 127 additions and 3 deletions

View File

@ -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);
}
/**

View File

@ -0,0 +1,102 @@
<?php
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
/**
* @group DDC-1998
*/
class DDC1998Test extends \Doctrine\Tests\OrmFunctionalTestCase
{
public function testSqlConversionAsIdentifier()
{
Type::addType('ddc1998', __NAMESPACE__ . '\\DDC1998Type');
$this->_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;
}
}