Skip to content

Commit eccd1da

Browse files
teohhanhuifabpot
authored andcommitted
[PropertyInfo] Support Doctrine custom mapping type in DoctrineExtractor
Also use Doctrine\DBAL\Types\Type class constants
1 parent 3dfe69f commit eccd1da

File tree

5 files changed

+147
-22
lines changed

5 files changed

+147
-22
lines changed

PropertyInfo/DoctrineExtractor.php

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
1515
use Doctrine\Common\Persistence\Mapping\MappingException;
16+
use Doctrine\DBAL\Types\Type as DBALType;
1617
use Doctrine\ORM\Mapping\ClassMetadataInfo;
1718
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
1819
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
@@ -93,23 +94,26 @@ public function getTypes($class, $property, array $context = array())
9394
$nullable = $metadata instanceof ClassMetadataInfo && $metadata->isNullable($property);
9495

9596
switch ($typeOfField) {
96-
case 'date':
97-
case 'datetime':
98-
case 'datetimetz':
99-
case 'time':
97+
case DBALType::DATE:
98+
case DBALType::DATETIME:
99+
case DBALType::DATETIMETZ:
100+
case 'vardatetime':
101+
case DBALType::TIME:
100102
return array(new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, 'DateTime'));
101103

102-
case 'array':
104+
case DBALType::TARRAY:
103105
return array(new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true));
104106

105-
case 'simple_array':
107+
case DBALType::SIMPLE_ARRAY:
106108
return array(new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING)));
107109

108-
case 'json_array':
110+
case DBALType::JSON_ARRAY:
109111
return array(new Type(Type::BUILTIN_TYPE_ARRAY, $nullable, null, true));
110112

111113
default:
112-
return array(new Type($this->getPhpType($typeOfField), $nullable));
114+
$builtinType = $this->getPhpType($typeOfField);
115+
116+
return $builtinType ? array(new Type($builtinType, $nullable)) : null;
113117
}
114118
}
115119
}
@@ -119,36 +123,37 @@ public function getTypes($class, $property, array $context = array())
119123
*
120124
* @param string $doctrineType
121125
*
122-
* @return string
126+
* @return string|null
123127
*/
124128
private function getPhpType($doctrineType)
125129
{
126130
switch ($doctrineType) {
127-
case 'smallint':
128-
// No break
129-
case 'bigint':
130-
// No break
131-
case 'integer':
131+
case DBALType::SMALLINT:
132+
case DBALType::BIGINT:
133+
case DBALType::INTEGER:
132134
return Type::BUILTIN_TYPE_INT;
133135

134-
case 'decimal':
136+
case DBALType::FLOAT:
137+
case DBALType::DECIMAL:
135138
return Type::BUILTIN_TYPE_FLOAT;
136139

137-
case 'text':
138-
// No break
139-
case 'guid':
140+
case DBALType::STRING:
141+
case DBALType::TEXT:
142+
case DBALType::GUID:
140143
return Type::BUILTIN_TYPE_STRING;
141144

142-
case 'boolean':
145+
case DBALType::BOOLEAN:
143146
return Type::BUILTIN_TYPE_BOOL;
144147

145-
case 'blob':
146-
// No break
148+
case DBALType::BLOB:
147149
case 'binary':
148150
return Type::BUILTIN_TYPE_RESOURCE;
149151

152+
case DBALType::OBJECT:
153+
return Type::BUILTIN_TYPE_OBJECT;
154+
150155
default:
151-
return $doctrineType;
156+
return;
152157
}
153158
}
154159
}

Tests/PropertyInfo/DoctrineExtractorTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Bridge\Doctrine\PropertyInfo\Tests;
1313

14+
use Doctrine\DBAL\Types\Type as DBALType;
1415
use Doctrine\ORM\EntityManager;
1516
use Doctrine\ORM\Tools\Setup;
1617
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
@@ -31,6 +32,11 @@ protected function setUp()
3132
$config = Setup::createAnnotationMetadataConfiguration(array(__DIR__.DIRECTORY_SEPARATOR.'Fixtures'), true);
3233
$entityManager = EntityManager::create(array('driver' => 'pdo_sqlite'), $config);
3334

35+
if (!DBALType::hasType('foo')) {
36+
DBALType::addType('foo', 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineFooType');
37+
$entityManager->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('custom_foo', 'foo');
38+
}
39+
3440
$this->extractor = new DoctrineExtractor($entityManager->getMetadataFactory());
3541
}
3642

@@ -45,6 +51,7 @@ public function testGetProperties()
4551
'simpleArray',
4652
'bool',
4753
'binary',
54+
'customFoo',
4855
'foo',
4956
'bar',
5057
),
@@ -78,6 +85,7 @@ public function typesProvider()
7885
new Type(Type::BUILTIN_TYPE_OBJECT, false, 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
7986
))),
8087
array('simpleArray', array(new Type(Type::BUILTIN_TYPE_ARRAY, false, null, true, new Type(Type::BUILTIN_TYPE_INT), new Type(Type::BUILTIN_TYPE_STRING)))),
88+
array('customFoo', null),
8189
array('notMapped', null),
8290
);
8391
}

Tests/PropertyInfo/Fixtures/DoctrineDummy.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,10 @@ class DoctrineDummy
7070
*/
7171
private $binary;
7272

73+
/**
74+
* @Column(type="custom_foo")
75+
*/
76+
private $customFoo;
77+
7378
public $notMapped;
7479
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
13+
14+
use Doctrine\DBAL\Platforms\AbstractPlatform;
15+
use Doctrine\DBAL\Types\ConversionException;
16+
use Doctrine\DBAL\Types\Type;
17+
18+
/**
19+
* @author Teoh Han Hui <[email protected]>
20+
*/
21+
class DoctrineFooType extends Type
22+
{
23+
/**
24+
* Type name.
25+
*/
26+
const NAME = 'foo';
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function getName()
32+
{
33+
return self::NAME;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
40+
{
41+
return $platform->getClobTypeDeclarationSQL(array());
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function convertToDatabaseValue($value, AbstractPlatform $platform)
48+
{
49+
if (null === $value) {
50+
return;
51+
}
52+
if (!$value instanceof Foo) {
53+
throw new ConversionException(sprintf('Expected %s, got %s', 'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\Foo', gettype($value)));
54+
}
55+
56+
return $foo->bar;
57+
}
58+
59+
/**
60+
* {@inheritdoc}
61+
*/
62+
public function convertToPHPValue($value, AbstractPlatform $platform)
63+
{
64+
if (null === $value) {
65+
return;
66+
}
67+
if (!is_string($value)) {
68+
throw ConversionException::conversionFailed($value, self::NAME);
69+
}
70+
71+
$foo = new Foo();
72+
$foo->bar = $value;
73+
74+
return $foo;
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
public function requiresSQLCommentHint(AbstractPlatform $platform)
81+
{
82+
return true;
83+
}
84+
}

Tests/PropertyInfo/Fixtures/Foo.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
13+
14+
/**
15+
* @author Teoh Han Hui <[email protected]>
16+
*/
17+
class Foo
18+
{
19+
/**
20+
* @var string
21+
*/
22+
public $bar;
23+
}

0 commit comments

Comments
 (0)