1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\StringType;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\Tests\OrmFunctionalTestCase;
/** @group DDC-2579 */
class DDC2579Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();
Type::addType(DDC2579Type::NAME, DDC2579Type::class);
$this->createSchemaForModels(
DDC2579Entity::class,
DDC2579EntityAssoc::class,
DDC2579AssocAssoc::class
);
}
public function testIssue(): void
{
$id = new DDC2579Id('foo');
$assoc = new DDC2579AssocAssoc($id);
$assocAssoc = new DDC2579EntityAssoc($assoc);
$entity = new DDC2579Entity($assocAssoc);
$repository = $this->_em->getRepository(DDC2579Entity::class);
$this->_em->persist($assoc);
$this->_em->persist($assocAssoc);
$this->_em->persist($entity);
$this->_em->flush();
$entity->value++;
$this->_em->persist($entity);
$this->_em->flush();
$this->_em->clear();
$id = $entity->id;
$value = $entity->value;
$criteria = ['assoc' => $assoc, 'id' => $id];
$entity = $repository->findOneBy($criteria);
self::assertInstanceOf(DDC2579Entity::class, $entity);
self::assertEquals($value, $entity->value);
$this->_em->remove($entity);
$this->_em->flush();
$this->_em->clear();
self::assertNull($repository->findOneBy($criteria));
self::assertCount(0, $repository->findAll());
}
}
/** @Entity */
class DDC2579Entity
{
/**
* @var DDC2579Id
* @Id
* @Column(type="ddc2579", length=255)
*/
public $id;
/**
* @var DDC2579EntityAssoc
* @Id
* @ManyToOne(targetEntity="DDC2579EntityAssoc")
* @JoinColumn(name="relation_id", referencedColumnName="association_id")
*/
public $assoc;
/**
* @var int
* @Column(type="integer")
*/
public $value;
public function __construct(DDC2579EntityAssoc $assoc, int $value = 0)
{
$this->id = $assoc->assocAssoc->associationId;
$this->assoc = $assoc;
$this->value = $value;
}
}
/** @Entity */
class DDC2579EntityAssoc
{
/**
* @var DDC2579AssocAssoc
* @Id
* @ManyToOne(targetEntity="DDC2579AssocAssoc")
* @JoinColumn(name="association_id", referencedColumnName="associationId")
*/
public $assocAssoc;
public function __construct(DDC2579AssocAssoc $assocAssoc)
{
$this->assocAssoc = $assocAssoc;
}
}
/** @Entity */
class DDC2579AssocAssoc
{
/**
* @var DDC2579Id
* @Id
* @Column(type="ddc2579", length=255)
*/
public $associationId;
public function __construct(DDC2579Id $id)
{
$this->associationId = $id;
}
}
class DDC2579Type extends StringType
{
public const NAME = 'ddc2579';
/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
return (string) $value;
}
/**
* {@inheritDoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return new DDC2579Id($value);
}
/**
* {@inheritdoc}
*/
public function getName()
{
return self::NAME;
}
}
class DDC2579Id
{
/** @var string */
private $val;
public function __construct(string $val)
{
$this->val = $val;
}
public function __toString(): string
{
return $this->val;
}
}
|