Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit40e3e45

Browse files
committed
[PropertyInfo] Support Doctrine custom mapping type in DoctrineExtractor
Also use Doctrine\DBAL\Types\Type class constants
1 parentf5a7ad8 commit40e3e45

File tree

5 files changed

+147
-22
lines changed

5 files changed

+147
-22
lines changed

‎src/Symfony/Bridge/Doctrine/PropertyInfo/DoctrineExtractor.php‎

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

1414
useDoctrine\Common\Persistence\Mapping\ClassMetadataFactory;
1515
useDoctrine\Common\Persistence\Mapping\MappingException;
16+
useDoctrine\DBAL\Types\TypeasDBALType;
1617
useDoctrine\ORM\Mapping\ClassMetadataInfo;
1718
useDoctrine\ORM\Mapping\MappingExceptionasOrmMappingException;
1819
useSymfony\Component\PropertyInfo\PropertyListExtractorInterface;
@@ -93,23 +94,26 @@ public function getTypes($class, $property, array $context = array())
9394
$nullable =$metadatainstanceof 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
returnarray(newType(Type::BUILTIN_TYPE_OBJECT,$nullable,'DateTime'));
101103

102-
case'array':
104+
caseDBALType::TARRAY:
103105
returnarray(newType(Type::BUILTIN_TYPE_ARRAY,$nullable,null,true));
104106

105-
case'simple_array':
107+
caseDBALType::SIMPLE_ARRAY:
106108
returnarray(newType(Type::BUILTIN_TYPE_ARRAY,$nullable,null,true,newType(Type::BUILTIN_TYPE_INT),newType(Type::BUILTIN_TYPE_STRING)));
107109

108-
case'json_array':
110+
caseDBALType::JSON_ARRAY:
109111
returnarray(newType(Type::BUILTIN_TYPE_ARRAY,$nullable,null,true));
110112

111113
default:
112-
returnarray(newType($this->getPhpType($typeOfField),$nullable));
114+
$builtinType =$this->getPhpType($typeOfField);
115+
116+
return$builtinType ?array(newType($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
privatefunctiongetPhpType($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+
caseDBALType::STRING:
141+
case DBALType::TEXT:
142+
caseDBALType::GUID:
140143
return Type::BUILTIN_TYPE_STRING;
141144

142-
case'boolean':
145+
caseDBALType::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
}

‎src/Symfony/Bridge/Doctrine/Tests/PropertyInfo/DoctrineExtractorTest.php‎

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

1212
namespaceSymfony\Bridge\Doctrine\PropertyInfo\Tests;
1313

14+
useDoctrine\DBAL\Types\TypeasDBALType;
1415
useDoctrine\ORM\EntityManager;
1516
useDoctrine\ORM\Tools\Setup;
1617
useSymfony\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 =newDoctrineExtractor($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
newType(Type::BUILTIN_TYPE_OBJECT,false,'Symfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures\DoctrineRelation')
7986
))),
8087
array('simpleArray',array(newType(Type::BUILTIN_TYPE_ARRAY,false,null,true,newType(Type::BUILTIN_TYPE_INT),newType(Type::BUILTIN_TYPE_STRING)))),
88+
array('customFoo',null),
8189
array('notMapped',null),
8290
);
8391
}

‎src/Symfony/Bridge/Doctrine/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 <fabien@symfony.com>
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+
namespaceSymfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
13+
14+
useDoctrine\DBAL\Platforms\AbstractPlatform;
15+
useDoctrine\DBAL\Types\ConversionException;
16+
useDoctrine\DBAL\Types\Type;
17+
18+
/**
19+
* @author Teoh Han Hui <teohhanhui@gmail.com>
20+
*/
21+
class DoctrineFooTypeextends Type
22+
{
23+
/**
24+
* Type name.
25+
*/
26+
constNAME ='foo';
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
publicfunctiongetName()
32+
{
33+
returnself::NAME;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
publicfunctiongetSQLDeclaration(array$fieldDeclaration,AbstractPlatform$platform)
40+
{
41+
return$platform->getClobTypeDeclarationSQL(array());
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
publicfunctionconvertToDatabaseValue($value,AbstractPlatform$platform)
48+
{
49+
if (null ===$value) {
50+
return;
51+
}
52+
if (!$valueinstanceof Foo) {
53+
thrownewConversionException(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+
publicfunctionconvertToPHPValue($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 =newFoo();
72+
$foo->bar =$value;
73+
74+
return$foo;
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
publicfunctionrequiresSQLCommentHint(AbstractPlatform$platform)
81+
{
82+
returntrue;
83+
}
84+
}
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 <fabien@symfony.com>
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+
namespaceSymfony\Bridge\Doctrine\Tests\PropertyInfo\Fixtures;
13+
14+
/**
15+
* @author Teoh Han Hui <teohhanhui@gmail.com>
16+
*/
17+
class Foo
18+
{
19+
/**
20+
* @var string
21+
*/
22+
public$bar;
23+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp