diff --git a/src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php b/src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php index 6e330e558a..ecd25088be 100644 --- a/src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php +++ b/src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php @@ -13,6 +13,7 @@ namespace Symfony\Bridge\Doctrine\PropertyInfo; use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory; use Doctrine\Common\Persistence\Mapping\MappingException; +use Doctrine\DBAL\Types\Type as DBALType; use Doctrine\ORM\Mapping\ClassMetadataInfo; use Doctrine\ORM\Mapping\MappingException as OrmMappingException; use Symfony\Component\PropertyInfo\PropertyListExtractorInterface; @@ -93,23 +94,26 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE $nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property); switch ($typeOfField) { - case 'date': - case 'datetime': - case 'datetimetz': - case 'time': + case DBALType::DATE: + case DBALType::DATETIME: + case DBALType::DATETIMETZ: + case 'vardatetime': + case DBALType::TIME: return array(new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime')); - case 'array': + case DBALType::TARRAY: return array(new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)); - case 'simple_array': + case DBALType::SIMPLE_ARRAY: return array(new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING))); - case 'json_array': + case DBALType::JSON_ARRAY: return array(new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true)); default: - return array(new Type($this->getPhpType($typeOfField), $nullable)); + $builtinType = $this->getPhpType($typeOfField); + + return $builtinType ? array(new Type($builtinType, $nullable)) : null; } } } @@ -119,36 +123,37 @@ class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeE * * @param string $doctrineType * - * @return string + * @return string|null */ private function getPhpType($doctrineType) { switch ($doctrineType) { - case 'smallint': - // No break - case 'bigint': - // No break - case 'integer': + case DBALType::SMALLINT: + case DBALType::BIGINT: + case DBALType::INTEGER: return Type::BUILTIN_TYPE_INT; - case 'decimal': + case DBALType::FLOAT: + case DBALType::DECIMAL: return Type::BUILTIN_TYPE_FLOAT; - case 'text': - // No break - case 'guid': + case DBALType::STRING: + case DBALType::TEXT: + case DBALType::GUID: return Type::BUILTIN_TYPE_STRING; - case 'boolean': + case DBALType::BOOLEAN: return Type::BUILTIN_TYPE_BOOL; - case 'blob': - // No break + case DBALType::BLOB: case 'binary': return Type::BUILTIN_TYPE_RESOURCE; + case DBALType::OBJECT: + return Type::BUILTIN_TYPE_OBJECT; + default: - return $doctrineType; + return; } } } diff --git a/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php b/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php index ac1bb5f96e..ca098b6fc8 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php +++ b/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php @@ -11,6 +11,7 @@ namespace Symfony\Bridge\Doctrine\PropertyInfo\Tests; +use Doctrine\DBAL\Types\Type as DBALType; use Doctrine\ORM\EntityManager; use Doctrine\ORM\Tools\Setup; use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor; @@ -31,6 +32,11 @@ class DoctrineExtractorTest extends \PHPUnit_Framework_TestCase $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'), true); $entityManager = EntityManager::create(array('driver' => 'pdo_sqlite'), $config); + if (!DBALType::hasType('foo')) { + DBALType::addType('foo', 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineFooType'); + $entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('custom_foo', 'foo'); + } + $this->extractor = new DoctrineExtractor($entityManager->getMetadataFactory()); } @@ -45,6 +51,7 @@ class DoctrineExtractorTest extends \PHPUnit_Framework_TestCase 'simpleArray', 'bool', 'binary', + 'customFoo', 'foo', 'bar', ), @@ -78,6 +85,7 @@ class DoctrineExtractorTest extends \PHPUnit_Framework_TestCase new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation') ))), array('simpleArray', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING)))), + array('customFoo', null), array('notMapped', null), ); } diff --git a/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineDummy.php b/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineDummy.php index 864bd78407..0b6b7bb544 100644 --- a/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineDummy.php +++ b/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineDummy.php @@ -70,5 +70,10 @@ class DoctrineDummy */ private $binary; + /** + * @Column(type="custom_foo") + */ + private $customFoo; + public $notMapped; } diff --git a/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineFooType.php b/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineFooType.php new file mode 100644 index 0000000000..394eaebdef --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/DoctrineFooType.php @@ -0,0 +1,84 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures; + +use Doctrine\DBAL\Platforms\AbstractPlatform; +use Doctrine\DBAL\Types\ConversionException; +use Doctrine\DBAL\Types\Type; + +/** + * @author Teoh Han Hui + */ +class DoctrineFooType extends Type +{ + /** + * Type name. + */ + const NAME = 'foo'; + + /** + * {@inheritdoc} + */ + public function getName() + { + return self::NAME; + } + + /** + * {@inheritdoc} + */ + public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) + { + return $platform->getClobTypeDeclarationSQL(array()); + } + + /** + * {@inheritdoc} + */ + public function convertToDatabaseValue($value, AbstractPlatform $platform) + { + if (null === $value) { + return; + } + if (!$value instanceof Foo) { + throw new ConversionException(sprintf('Expected %s, got %s', 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\Foo', gettype($value))); + } + + return $foo->bar; + } + + /** + * {@inheritdoc} + */ + public function convertToPHPValue($value, AbstractPlatform $platform) + { + if (null === $value) { + return; + } + if (!is_string($value)) { + throw ConversionException::conversionFailed($value, self::NAME); + } + + $foo = new Foo(); + $foo->bar = $value; + + return $foo; + } + + /** + * {@inheritdoc} + */ + public function requiresSQLCommentHint(AbstractPlatform $platform) + { + return true; + } +} diff --git a/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/Foo.php b/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/Foo.php new file mode 100644 index 0000000000..3e4016cc56 --- /dev/null +++ b/src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/Foo.php @@ -0,0 +1,23 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures; + +/** + * @author Teoh Han Hui + */ +class Foo +{ + /** + * @var string + */ + public $bar; +}