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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
<?php
declare(strict_types=1);
namespace Doctrine\Tests\ORM\Functional;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\Tests\OrmFunctionalTestCase;
use PHPUnit\Framework\Attributes\Group;
#[Group('DDC-952')]
class OneToOneEagerLoadingTest extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();
$this->createSchemaForModels(
Train::class,
TrainDriver::class,
TrainOwner::class,
Waggon::class,
TrainOrder::class,
);
}
#[Group('non-cacheable')]
public function testEagerLoadOneToOneOwningSide(): void
{
$train = new Train(new TrainOwner('Alexander'));
$driver = new TrainDriver('Benjamin');
$waggon = new Waggon();
$train->setDriver($driver);
$train->addWaggon($waggon);
$this->_em->persist($train); // cascades
$this->_em->flush();
$this->_em->clear();
$this->getQueryLog()->reset()->enable();
$train = $this->_em->find($train::class, $train->id);
self::assertFalse($this->isUninitializedObject($train->driver));
self::assertEquals('Benjamin', $train->driver->name);
$this->assertQueryCount(1);
}
#[Group('non-cacheable')]
public function testEagerLoadOneToOneNullOwningSide(): void
{
$train = new Train(new TrainOwner('Alexander'));
$this->_em->persist($train); // cascades
$this->_em->flush();
$this->_em->clear();
$this->getQueryLog()->reset()->enable();
$train = $this->_em->find($train::class, $train->id);
self::assertNull($train->driver);
$this->assertQueryCount(1);
}
#[Group('non-cacheable')]
public function testEagerLoadOneToOneInverseSide(): void
{
$owner = new TrainOwner('Alexander');
$train = new Train($owner);
$this->_em->persist($train); // cascades
$this->_em->flush();
$this->_em->clear();
$this->getQueryLog()->reset()->enable();
$this->_em->find($owner::class, $owner->id);
self::assertFalse($this->isUninitializedObject($owner->train));
self::assertInstanceOf(Train::class, $owner->train);
$this->assertQueryCount(1);
}
#[Group('non-cacheable')]
public function testEagerLoadOneToOneNullInverseSide(): void
{
$driver = new TrainDriver('Dagny Taggert');
$this->_em->persist($driver);
$this->_em->flush();
$this->_em->clear();
self::assertNull($driver->train);
$this->getQueryLog()->reset()->enable();
$driver = $this->_em->find($driver::class, $driver->id);
self::assertNull($driver->train);
$this->assertQueryCount(1);
}
public function testEagerLoadManyToOne(): void
{
$train = new Train(new TrainOwner('Alexander'));
$waggon = new Waggon();
$train->addWaggon($waggon);
$this->_em->persist($train); // cascades
$this->_em->flush();
$this->_em->clear();
$waggon = $this->_em->find($waggon::class, $waggon->id);
self::assertFalse($this->isUninitializedObject($waggon->train));
self::assertInstanceOf(Train::class, $waggon->train);
}
#[Group('non-cacheable')]
public function testEagerLoadWithNullableColumnsGeneratesLeftJoinOnBothSides(): void
{
$train = new Train(new TrainOwner('Alexander'));
$driver = new TrainDriver('Benjamin');
$this->_em->persist($train);
$this->_em->persist($driver);
$this->_em->flush();
$trainId = $train->id;
$driverId = $driver->id;
$this->_em->clear();
$train = $this->_em->find($train::class, $train->id);
self::assertNotNull($train, 'It should be possible to find the train even though it has no driver');
self::assertSame($trainId, $train->id);
$this->_em->clear();
$driver = $this->_em->find($driver::class, $driver->id);
self::assertNotNull($driver, 'It should be possible to find the driver even though they drive no train');
self::assertSame($driverId, $driver->id);
}
#[Group('non-cacheable')]
public function testEagerLoadWithNonNullableColumnsGeneratesInnerJoinOnOwningSide(): void
{
$waggon = new Waggon();
// It should have a train
$train = new Train(new TrainOwner('Alexander'));
$train->addWaggon($waggon);
$this->_em->persist($train);
$this->_em->flush();
$this->_em->clear();
$waggon = $this->_em->find($waggon::class, $waggon->id);
// The last query is the eager loading of the owner of the train
$this->assertSQLEquals(
'SELECT t0.id AS id_1, t0.name AS name_2, t3.id AS id_4, t3.driver_id AS driver_id_5, t3.owner_id AS owner_id_6 FROM TrainOwner t0 LEFT JOIN Train t3 ON t3.owner_id = t0.id WHERE t0.id IN (?)',
$this->getLastLoggedQuery()['sql'],
);
// The one before is the fetching of the waggon and train
$this->assertSQLEquals(
'SELECT t0.id AS id_1, t0.train_id AS train_id_2, t3.id AS id_4, t3.driver_id AS driver_id_5, t3.owner_id AS owner_id_6 FROM Waggon t0 INNER JOIN Train t3 ON t0.train_id = t3.id WHERE t0.id = ?',
$this->getLastLoggedQuery(1)['sql'],
);
}
#[Group('non-cacheable')]
public function testEagerLoadWithNonNullableColumnsGeneratesLeftJoinOnNonOwningSide(): void
{
$owner = new TrainOwner('Alexander');
$this->_em->persist($owner);
$this->_em->flush();
$this->_em->clear();
$owner = $this->_em->find($owner::class, $owner->id);
self::assertNotNull($owner, 'An owner without a train should be able to exist.');
}
#[Group('DDC-1946')]
public function testEagerLoadingDoesNotBreakRefresh(): void
{
$train = new Train(new TrainOwner('Johannes'));
$order = new TrainOrder($train);
$this->_em->persist($train);
$this->_em->persist($order);
$this->_em->flush();
$this->_em->getConnection()->executeStatement('UPDATE TrainOrder SET train_id = NULL');
self::assertSame($train, $order->train);
$this->_em->refresh($order);
self::assertNull($order->train, 'Train reference was not refreshed to NULL.');
}
}
#[Entity]
class Train
{
/** @var int */
#[Id]
#[Column(type: 'integer')]
#[GeneratedValue]
public $id;
/**
* Owning side
*
* @var TrainDriver
*/
#[OneToOne(targetEntity: 'TrainDriver', inversedBy: 'train', fetch: 'EAGER', cascade: ['persist'])]
#[JoinColumn(nullable: true)]
public $driver;
/**
* Owning side
*
* @var TrainOwner
*/
#[OneToOne(targetEntity: 'TrainOwner', inversedBy: 'train', fetch: 'EAGER', cascade: ['persist'])]
#[JoinColumn(nullable: false)]
public $owner;
/** @phpstan-var Collection<int, Waggon> */
#[OneToMany(targetEntity: 'Waggon', mappedBy: 'train', cascade: ['persist'])]
public $waggons;
public function __construct(TrainOwner $owner)
{
$this->waggons = new ArrayCollection();
$this->setOwner($owner);
}
public function setDriver(TrainDriver $driver): void
{
$this->driver = $driver;
$driver->setTrain($this);
}
public function setOwner(TrainOwner $owner): void
{
$this->owner = $owner;
$owner->setTrain($this);
}
public function addWaggon(Waggon $w): void
{
$w->setTrain($this);
$this->waggons[] = $w;
}
}
#[Entity]
class TrainDriver
{
/** @var int */
#[Id]
#[Column(type: 'integer')]
#[GeneratedValue]
public $id;
/**
* Inverse side
*
* @var Train
*/
#[OneToOne(targetEntity: 'Train', mappedBy: 'driver', fetch: 'EAGER')]
public $train;
public function __construct(
#[Column(type: 'string', length: 255)]
public string $name,
) {
}
public function setTrain(Train $t): void
{
$this->train = $t;
}
}
#[Entity]
class TrainOwner
{
/** @var int */
#[Id]
#[Column(type: 'integer')]
#[GeneratedValue]
public $id;
/**
* Inverse side
*
* @var Train
*/
#[OneToOne(targetEntity: 'Train', mappedBy: 'owner', fetch: 'EAGER')]
public $train;
public function __construct(
#[Column(type: 'string', length: 255)]
public string $name,
) {
}
public function setTrain(Train $t): void
{
$this->train = $t;
}
}
#[Entity]
class Waggon
{
/** @var int */
#[Id]
#[GeneratedValue]
#[Column(type: 'integer')]
public $id;
/** @var Train */
#[ManyToOne(targetEntity: 'Train', inversedBy: 'waggons', fetch: 'EAGER')]
#[JoinColumn(nullable: false)]
public $train;
public function setTrain($train): void
{
$this->train = $train;
}
}
#[Entity]
class TrainOrder
{
/** @var int */
#[Id]
#[GeneratedValue]
#[Column(type: 'integer')]
public $id;
public function __construct(
#[OneToOne(targetEntity: 'Train', fetch: 'EAGER')]
public Train|null $train = null,
) {
}
}
|