From 4510f5a5b84af625d2db6da985e4e74124c4eadc Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sun, 30 Sep 2012 15:40:19 -0300 Subject: [PATCH 1/2] Fix DDC-2012 --- .../Persisters/JoinedSubclassPersister.php | 3 +- .../ORM/Functional/Ticket/DDC2012Test.php | 177 ++++++++++++++++++ 2 files changed, 179 insertions(+), 1 deletion(-) create mode 100755 tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2012Test.php diff --git a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php index 431b237a6..e9d214311 100644 --- a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php +++ b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php @@ -468,7 +468,8 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister } } else if ($this->_class->name != $this->_class->rootEntityName || ! $this->_class->isIdGeneratorIdentity() || $this->_class->identifier[0] != $name) { - $columns[] = $this->quoteStrategy->getColumnName($name, $this->_class, $this->_platform); + $columns[] = $this->quoteStrategy->getColumnName($name, $this->_class, $this->_platform); + $this->_columnTypes[$name] = $this->_class->fieldMappings[$name]['type']; } } diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2012Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2012Test.php new file mode 100755 index 000000000..30de0deb3 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2012Test.php @@ -0,0 +1,177 @@ +_schemaTool->createSchema(array( + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2012Item'), + $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC2012ItemPerson'), + )); + } + + public function testIssue() + { + $item = new DDC2012ItemPerson(); + $item->tsv = array('word1', 'word2', 'word3'); + + $this->_em->persist($item); + $this->_em->flush(); + $this->_em->clear(); + + $item = $this->_em->find(get_class($item), $item->id); + + $this->assertArrayHasKey('convertToDatabaseValueSQL', DDC2012TsVectorType::$calls); + $this->assertArrayHasKey('convertToDatabaseValue', DDC2012TsVectorType::$calls); + $this->assertArrayHasKey('convertToPHPValue', DDC2012TsVectorType::$calls); + + $this->assertCount(1, DDC2012TsVectorType::$calls['convertToDatabaseValueSQL']); + $this->assertCount(1, DDC2012TsVectorType::$calls['convertToDatabaseValue']); + $this->assertCount(1, DDC2012TsVectorType::$calls['convertToPHPValue']); + + $this->assertInstanceOf(__NAMESPACE__ . '\DDC2012Item', $item); + $this->assertEquals(array('word1', 'word2', 'word3'), $item->tsv); + + + $item->tsv = array('word1', 'word2'); + + $this->_em->persist($item); + $this->_em->flush(); + $this->_em->clear(); + + $item = $this->_em->find(get_class($item), $item->id); + + $this->assertCount(2, DDC2012TsVectorType::$calls['convertToDatabaseValueSQL']); + $this->assertCount(2, DDC2012TsVectorType::$calls['convertToDatabaseValue']); + $this->assertCount(2, DDC2012TsVectorType::$calls['convertToPHPValue']); + + $this->assertInstanceOf(__NAMESPACE__ . '\DDC2012Item', $item); + $this->assertEquals(array('word1', 'word2'), $item->tsv); + } +} + +/** + * @Table(name="ddc2010_item") + * @Entity + * @InheritanceType("JOINED") + * @DiscriminatorColumn(name="type_id", type="smallint") + * @DiscriminatorMap({ + * 1 = "DDC2012ItemPerson" + * }) + */ +class DDC2012Item +{ + /** + * @Id + * @GeneratedValue + * @Column(type="integer") + */ + public $id; + + /** + * @Column(name="tsv", type="tsvector", nullable=true) + */ + public $tsv; +} + +/** + * @Table(name="ddc2010_item_person") + * @Entity + */ +class DDC2012ItemPerson extends DDC2012Item +{ + +} + +class DDC2012TsVectorType extends Type +{ + const MYTYPE = 'tsvector'; + + public static $calls = array(); + + /** + * {@inheritdoc} + */ + public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) + { + return $platform->getBlobTypeDeclarationSQL($fieldDeclaration); + } + + /** + * {@inheritdoc} + */ + public function convertToDatabaseValue($value, AbstractPlatform $platform) + { + if (is_array($value)) { + $value = implode(" ", $value); + } + + self::$calls[__FUNCTION__][] = array( + 'value' => $value, + 'platform' => $platform, + ); + + return $value; + } + + /** + * {@inheritdoc} + */ + public function convertToPHPValue($value, AbstractPlatform $platform) + { + self::$calls[__FUNCTION__][] = array( + 'value' => $value, + 'platform' => $platform, + ); + + return explode(" ", strtolower($value)); + } + + /** + * {@inheritdoc} + */ + public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform) + { + self::$calls[__FUNCTION__][] = array( + 'sqlExpr' => $sqlExpr, + 'platform' => $platform, + ); + + // changed to upper expression to keep the teste compatible with other Databases + //sprintf('to_tsvector(%s)', $sqlExpr); + + return $platform->getUpperExpression($sqlExpr); + } + + /** + * {@inheritdoc} + */ + public function canRequireSQLConversion() + { + return true; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return self::MYTYPE; + } +} \ No newline at end of file From 5cb4466f7c95a4490db683907e84691c9a868233 Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Sun, 30 Sep 2012 15:47:00 -0300 Subject: [PATCH 2/2] Fix test case --- tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2012Test.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2012Test.php b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2012Test.php index 30de0deb3..eca15cea3 100755 --- a/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2012Test.php +++ b/tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2012Test.php @@ -110,7 +110,7 @@ class DDC2012TsVectorType extends Type */ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) { - return $platform->getBlobTypeDeclarationSQL($fieldDeclaration); + return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration); } /** @@ -153,7 +153,7 @@ class DDC2012TsVectorType extends Type 'platform' => $platform, ); - // changed to upper expression to keep the teste compatible with other Databases + // changed to upper expression to keep the test compatible with other Databases //sprintf('to_tsvector(%s)', $sqlExpr); return $platform->getUpperExpression($sqlExpr);