Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lazy and eager collection refresh inconsistency #10065

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -2473,13 +2473,13 @@ private function doRefresh($entity, array &$visited, ?int $lockMode = null): voi
throw ORMInvalidArgumentException::entityNotManaged($entity);
}

$this->cascadeRefresh($entity, $visited, $lockMode);

$this->getEntityPersister($class->name)->refresh(
array_combine($class->getIdentifierFieldNames(), $this->entityIdentifiers[$oid]),
$entity,
$lockMode
);

$this->cascadeRefresh($entity, $visited, $lockMode);
}

/**
Expand Down
161 changes: 161 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/LazyEagerCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\Tests\OrmFunctionalTestCase;

class LazyEagerCollectionTest extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->createSchemaForModels(
LazyEagerCollectionUser::class,
LazyEagerCollectionAddress::class,
LazyEagerCollectionPhone::class
);
}

public function testRefreshRefreshesBothLazyAndEagerCollections(): void
{
$user = new LazyEagerCollectionUser();
$user->data = 'Guilherme';

$ph = new LazyEagerCollectionPhone();
$ph->data = '12345';
$user->addPhone($ph);

$ad = new LazyEagerCollectionAddress();
$ad->data = '6789';
$user->addAddress($ad);

$this->_em->persist($user);
$this->_em->persist($ad);
$this->_em->persist($ph);
$this->_em->flush();
$this->_em->clear();

$user = $this->_em->find(LazyEagerCollectionUser::class, $user->id);
$ph = $user->phones[0];
$ad = $user->addresses[0];

$ph->data = 'abc';
$ad->data = 'def';

$this->_em->refresh($user);

self::assertSame('12345', $ph->data);
self::assertSame('6789', $ad->data);
}
}

/**
* @Entity
*/
class LazyEagerCollectionUser
{
/**
* @var int
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;

/**
* @var string
* @Column(type="string", length=255)
*/
public $data;

/**
* @ORM\OneToMany(targetEntity="LazyEagerCollectionPhone", cascade={"refresh"}, fetch="EAGER", mappedBy="user")
*
* @var LazyEagerCollectionPhone[]
*/
public $phones;

/**
* @ORM\OneToMany(targetEntity="LazyEagerCollectionAddress", cascade={"refresh"}, mappedBy="user")
*
* @var LazyEagerCollectionAddress[]
*/
public $addresses;

public function __construct()
{
$this->addresses = new ArrayCollection();
$this->phones = new ArrayCollection();
}

public function addPhone(LazyEagerCollectionPhone $phone): void
{
$phone->user = $this;
$this->phones[] = $phone;
}

public function addAddress(LazyEagerCollectionAddress $address): void
{
$address->user = $this;
$this->addresses[] = $address;
}
}

/** @Entity */
class LazyEagerCollectionPhone
{
/**
* @var int
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;

/**
* @var string
* @Column(type="string", length=255)
*/
public $data;

/**
* @ORM\ManyToOne(targetEntity="LazyEagerCollectionUser", inversedBy="phones")
*
* @var LazyEagerCollectionUser
*/
public $user;
}

/** @Entity */
class LazyEagerCollectionAddress
{
/**
* @var int
* @Id
* @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
public $id;

/**
* @var string
* @Column(type="string", length=255)
*/
public $data;

/**
* @ORM\ManyToOne(targetEntity="LazyEagerCollectionUser", inversedBy="addresses")
*
* @var LazyEagerCollectionUser
*/
public $user;
}
Loading