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

Cascade detach should detach lazy collections too #11717

Open
wants to merge 2 commits into
base: 2.20.x
Choose a base branch
from

Conversation

goetas
Copy link
Member

@goetas goetas commented Nov 15, 2024

This is a follow up of #10065

When a collection is "LAZY", it is not being detached when detach is called.

In order to detach all the entities, the collection needs to be initialized (similarly to what is done for remove()).

To give a more real example:

Given this class:

class User
{
    /**
     * @ORM\OneToMany(targetEntity="Address", cascade={"detach"}, fetch="LAZY")
     */
    public $addresses;
}

before my change

$user = $em->find(User::class, $user->id);
$em->detach($user);

$adr1 = $user->addresses[0]; 
// this will trigger an SQL query to fin the address 
// but it should not happen because the user is detached, addresses should be detached as well

$m->contains($adr1); // is managed ... but it should have been detached

after my change

$user = $em->find(User::class, $user->id);
$em->detach($user);

$adr1 = $user->addresses[0];  // no SQL triggered at this point

$m->contains($adr1); // not managed

@goetas goetas force-pushed the cascade-detach-lazy-refresh branch 2 times, most recently from d7d1e4c to 4c3c8f0 Compare November 15, 2024 21:46
if ($this->isInIdentityMap($entity)) {
$this->removeFromIdentityMap($entity);
}
$state = $this->getEntityState($entity, self::STATE_DETACHED);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to make this changes because "child" objects should be detached first, and then the parents (otherwise the IDs are not available for some DBs as sqlite)

Copy link
Member

@SenseException SenseException left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your example is describing that there shouldn't be an SQL query triggered for addresses when detached. Shouldn't the test make sure that this is the case?

$adr1 = $user->addresses[0];  // no SQL triggered at this point

When I run $this->getQueryLog() after $ad = $user->addresses[0], I find the following SQL query in the logger:

SELECT t0.id AS id_1, t0.data AS data_2, t0.user_id AS user_id_3 FROM LazyEagerCollectionAddress t0 WHERE t0.user_id = ?

If I understood the PR correctly we have two goals: detach the lazy stuff and prevent an SQL query for the lazy objects. Am I understanding it correctly that this query shouldn't happen in the test?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants