1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ORM\Functional; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
6
|
|
|
use Doctrine\DBAL\Types\Type; |
|
|
|
|
7
|
|
|
use Doctrine\Tests\DbalTypes\CustomIdObject; |
8
|
|
|
use Doctrine\Tests\OrmFunctionalTestCase; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Functional tests for the Class Table Inheritance mapping strategy with custom id object types. |
12
|
|
|
* |
13
|
|
|
* @group GH5988 |
14
|
|
|
*/ |
15
|
|
|
final class GH5988Test extends OrmFunctionalTestCase |
16
|
|
|
{ |
17
|
|
|
protected function setUp() |
18
|
|
|
{ |
19
|
|
|
parent::setUp(); |
20
|
|
|
|
21
|
|
|
if (! Type::hasType(GH5988CustomIdObjectHashType::NAME)) { |
22
|
|
|
Type::addType(GH5988CustomIdObjectHashType::NAME, GH5988CustomIdObjectHashType::class); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$this->setUpEntitySchema([GH5988CustomIdObjectTypeParent::class, GH5988CustomIdObjectTypeChild::class]); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testDelete() |
29
|
|
|
{ |
30
|
|
|
$object = new GH5988CustomIdObjectTypeChild(new CustomIdObject('foo'), 'Test'); |
31
|
|
|
|
32
|
|
|
$this->_em->persist($object); |
33
|
|
|
$this->_em->flush(); |
34
|
|
|
|
35
|
|
|
$id = $object->id; |
36
|
|
|
|
37
|
|
|
$object2 = $this->_em->find(GH5988CustomIdObjectTypeChild::class, $id); |
38
|
|
|
|
39
|
|
|
$this->_em->remove($object2); |
40
|
|
|
$this->_em->flush(); |
41
|
|
|
|
42
|
|
|
self::assertNull($this->_em->find(GH5988CustomIdObjectTypeChild::class, $id)); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
class GH5988CustomIdObjectHashType extends Type |
48
|
|
|
{ |
49
|
|
|
const NAME = 'CustomIdObjectHash'; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
55
|
|
|
{ |
56
|
|
|
return $value->id . '_test'; |
57
|
|
|
} |
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function convertToPHPValue($value, AbstractPlatform $platform) |
62
|
|
|
{ |
63
|
|
|
return new CustomIdObject(str_replace('_test', '', $value)); |
64
|
|
|
} |
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
|
|
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) |
69
|
|
|
{ |
70
|
|
|
return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration); |
71
|
|
|
} |
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function getName() |
76
|
|
|
{ |
77
|
|
|
return self::NAME; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @Entity |
83
|
|
|
* @Table |
84
|
|
|
* @InheritanceType("JOINED") |
85
|
|
|
* @DiscriminatorColumn(name="type", type="string") |
86
|
|
|
* @DiscriminatorMap({"child" = GH5988CustomIdObjectTypeChild::class}) |
87
|
|
|
*/ |
88
|
|
|
abstract class GH5988CustomIdObjectTypeParent |
89
|
|
|
{ |
90
|
|
|
/** |
91
|
|
|
* @Id |
92
|
|
|
* @Column(type="CustomIdObjectHash") |
93
|
|
|
* |
94
|
|
|
* @var CustomIdObject |
95
|
|
|
*/ |
96
|
|
|
public $id; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @Entity |
102
|
|
|
* @Table |
103
|
|
|
*/ |
104
|
|
|
class GH5988CustomIdObjectTypeChild extends GH5988CustomIdObjectTypeParent |
105
|
|
|
{ |
106
|
|
|
/** @var string */ |
107
|
|
|
public $name; |
108
|
|
|
|
109
|
|
|
public function __construct(CustomIdObject $id, string $name) |
110
|
|
|
{ |
111
|
|
|
$this->id = $id; |
112
|
|
|
$this->name = $name; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: