8000 [PropertyInfo] Support Doctrine custom mapping type in DoctrineExtractor by teohhanhui · Pull Request #18209 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[PropertyInfo] Support Doctrine custom mapping type in DoctrineExtractor 8000 #18209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 27 additions & 22 deletions src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

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;
Expand Down Expand Up @@ -93,23 +94,26 @@ public function getTypes($class, $property, array $context = array())
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);

switch ($typeOfField) {
case 'date':
case 'datetime':
case 'datetimetz':
case 'time':
case DBALType::DATE:
8000 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;
}
}
}
Expand All @@ -119,36 +123,37 @@ public function getTypes($class, $property, array $context = array())
*
* @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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +32,11 @@ protected function setUp()
$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());
}

Expand All @@ -45,6 +51,7 @@ public function testGetProperties()
'simpleArray',
'bool',
'binary',
'customFoo',
'foo',
'bar',
),
Expand Down Expand Up @@ -78,6 +85,7 @@ public function typesProvider()
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),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,10 @@ class DoctrineDummy
*/
private $binary;

/**
* @Column(type="custom_foo")
*/
private $customFoo;

public $notMapped;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <teohhanhui@gmail.com>
*/
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;
}
}
23 changes: 23 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/Fixtures/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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 <teohhanhui@gmail.com>
*/
class Foo
{
/**
* @var string
*/
public $bar;
}
0