DDC-353: Doctrine thrown exception, when it is trying to remove not initialized proxy. #439

Closed
opened 2026-01-22 12:38:15 +01:00 by admin · 15 comments
Owner

Originally created by @doctrinebot on GitHub (Feb 20, 2010).

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user cloun:

Introduction:
I have a Product, which has many (OneToMany collection) Pictures. Picture has a File (ManyToOne with cascade={"remove", "persist"}).
The problem:
When I remove a picture from a product

$product->pictures->removeElement($picture);
$picture->resetProduct();

doctrine removes picture, then cascade removes file, but file is not yet initialized. Doctrine tries to check entity state:

    public function getEntityState($entity, $assume = null)
    {
        $oid = spl*object*hash($entity);
        if ( ! isset($this->_entityStates[$oid])) {
...
            if ($assume === null) {
                if ($this->*em->getClassMetadata(get*class($entity))->getIdentifierValues($entity)) {
                    $this->*entityStates[$oid] = self::STATE*DETACHED;
...

The getIdentifierValues method tries to take entity id through reflection, but id field is absent.
I think that the getEntityState method should support case when entity is instance of IProxy...

Originally created by @doctrinebot on GitHub (Feb 20, 2010). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user cloun: Introduction: I have a Product, which has many (OneToMany collection) Pictures. Picture has a File (ManyToOne with cascade={"remove", "persist"}). The problem: When I remove a picture from a product ``` $product->pictures->removeElement($picture); $picture->resetProduct(); ``` doctrine removes picture, then cascade removes file, but file is not yet initialized. Doctrine tries to check entity state: ``` public function getEntityState($entity, $assume = null) { $oid = spl*object*hash($entity); if ( ! isset($this->_entityStates[$oid])) { ... if ($assume === null) { if ($this->*em->getClassMetadata(get*class($entity))->getIdentifierValues($entity)) { $this->*entityStates[$oid] = self::STATE*DETACHED; ... ``` The getIdentifierValues method tries to take entity id through reflection, but id field is absent. I think that the getEntityState method should support case when entity is instance of IProxy...
admin added the Bug label 2026-01-22 12:38:15 +01:00
admin closed this issue 2026-01-22 12:38:16 +01:00
Author
Owner

@doctrinebot commented on GitHub (Feb 20, 2010):

Comment created by cloun:

May be I'm wrong, but as workaround I made the following:

  1. Added a _load method to Proxy interface (why it is not called IProxy?)
  2. Made the _load method public (in ProxyFactory)
  3. Added another one case to getEntityState method:
            if ($entity instanceof \Doctrine\ORM\Proxy\Proxy) {
                $entity->_load();
            } elseif ($assume === null) {
@doctrinebot commented on GitHub (Feb 20, 2010): Comment created by cloun: May be I'm wrong, but as workaround I made the following: 1. Added a _load method to Proxy interface (why it is not called IProxy?) 2. Made the _load method public (in ProxyFactory) 3. Added another one case to getEntityState method: ``` if ($entity instanceof \Doctrine\ORM\Proxy\Proxy) { $entity->_load(); } elseif ($assume === null) { ```
Author
Owner

@doctrinebot commented on GitHub (Feb 23, 2010):

Comment created by romanb:

What is the error/exception you are getting? Please show the full stack trace in case of an exception.

@doctrinebot commented on GitHub (Feb 23, 2010): Comment created by romanb: What is the error/exception you are getting? Please show the full stack trace in case of an exception.
Author
Owner

@doctrinebot commented on GitHub (Feb 23, 2010):

Comment created by romanb:

This code:

$product->pictures->removeElement($picture);
$picture->resetProduct();

will only remove the association, not the picture or the file, thus remove cascades would not even apply.

@doctrinebot commented on GitHub (Feb 23, 2010): Comment created by romanb: This code: ``` $product->pictures->removeElement($picture); $picture->resetProduct(); ``` will only remove the association, not the picture or the file, thus remove cascades would not even apply.
Author
Owner

@doctrinebot commented on GitHub (Feb 23, 2010):

Comment created by romanb:

Please show the full code you're using (models etc.) and the error or unexpected behavior you're getting, so that we can try to reproduce it.

@doctrinebot commented on GitHub (Feb 23, 2010): Comment created by romanb: Please show the full code you're using (models etc.) and the error or unexpected behavior you're getting, so that we can try to reproduce it.
Author
Owner

@doctrinebot commented on GitHub (Feb 23, 2010):

Comment created by cloun:

It will take many time, because my project has a huge infrastructure. I write when I reproduce it in console.

@doctrinebot commented on GitHub (Feb 23, 2010): Comment created by cloun: It will take many time, because my project has a huge infrastructure. I write when I reproduce it in console.
Author
Owner

@doctrinebot commented on GitHub (Feb 26, 2010):

Comment created by cloun:

I have attached test project in which I reproduced this problem.
What I did:

  1. I created schema manually (user create.sql file)
  2. I generated model classes (by doctrine-cli.php script)
  3. Made a little changes in annotations (I just added OneToMany between product and picture)
  4. Run script pictest.php
    Steps for reproducing:
  5. create schema from create.sql file
  6. fix connection string in dbconfig.php
  7. run pictest.sql
@doctrinebot commented on GitHub (Feb 26, 2010): Comment created by cloun: I have attached test project in which I reproduced this problem. What I did: 1. I created schema manually (user create.sql file) 2. I generated model classes (by doctrine-cli.php script) 3. Made a little changes in annotations (I just added OneToMany between product and picture) 4. Run script pictest.php Steps for reproducing: 1. create schema from create.sql file 2. fix connection string in dbconfig.php 3. run pictest.sql
Author
Owner

@doctrinebot commented on GitHub (Feb 26, 2010):

Comment created by @beberlei:

Please create a unit-test, in the schema of tests\Doctrine\Tests\ORM\Functional\Ticket and attach it as a .patch or .diff or .php file. This is not a minimal re-produce case that can be easily understood or run by us.

@doctrinebot commented on GitHub (Feb 26, 2010): Comment created by @beberlei: Please create a unit-test, in the schema of tests\Doctrine\Tests\ORM\Functional\Ticket and attach it as a .patch or .diff or .php file. This is not a minimal re-produce case that can be easily understood or run by us.
Author
Owner

@doctrinebot commented on GitHub (Feb 27, 2010):

Comment created by cloun:

Could you please provide some kind of documentation (or link to it) on how to create such unit tests.
I understand, that my test enviroment is not convenient for you, but it countains only initialization code and issue code. It's a minimal code which required for reproducing. If you spend a little time, you would see...

@doctrinebot commented on GitHub (Feb 27, 2010): Comment created by cloun: Could you please provide some kind of documentation (or link to it) on how to create such unit tests. I understand, that my test enviroment is not convenient for you, but it countains only initialization code and issue code. It's a minimal code which required for reproducing. If you spend a little time, you would see...
Author
Owner

@doctrinebot commented on GitHub (Feb 28, 2010):

Comment created by @beberlei:

Verified, attached the testcase

@Valery: The attached file shows the test-case for your behaviour, its a stripped down 200 lines php file compared to your 30kb rar file :-)

@doctrinebot commented on GitHub (Feb 28, 2010): Comment created by @beberlei: Verified, attached the testcase @Valery: The attached file shows the test-case for your behaviour, its a stripped down 200 lines php file compared to your 30kb rar file :-)
Author
Owner

@doctrinebot commented on GitHub (Feb 28, 2010):

Comment created by @beberlei:

The problem is easily explained:

Reference / Proxy objects are not marked as MANAGED in the UnitOfWork if they are initialized through a hydrator compared to calling getReference()

@doctrinebot commented on GitHub (Feb 28, 2010): Comment created by @beberlei: The problem is easily explained: Reference / Proxy objects are not marked as MANAGED in the UnitOfWork if they are initialized through a hydrator compared to calling `getReference()`
Author
Owner

@doctrinebot commented on GitHub (Feb 28, 2010):

Comment created by @beberlei:

Updated test-case that shows when it works, when it fails.

@doctrinebot commented on GitHub (Feb 28, 2010): Comment created by @beberlei: Updated test-case that shows when it works, when it fails.
Author
Owner

@doctrinebot commented on GitHub (Feb 28, 2010):

Comment created by @beberlei:

Fixed

@doctrinebot commented on GitHub (Feb 28, 2010): Comment created by @beberlei: Fixed
Author
Owner

@doctrinebot commented on GitHub (Feb 28, 2010):

Issue was closed with resolution "Fixed"

@doctrinebot commented on GitHub (Feb 28, 2010): Issue was closed with resolution "Fixed"
Author
Owner

@doctrinebot commented on GitHub (Mar 1, 2010):

Comment created by cloun:

Thanks for the fix!
@Benjamin Eberlei: I see. But know nothing about how you are writing a test-cases. May be it would be useful to provide documentation (simple instruction) about it.Possible, then my experience will bring a little more benefits for you..

@doctrinebot commented on GitHub (Mar 1, 2010): Comment created by cloun: Thanks for the fix! @Benjamin Eberlei: I see. But know nothing about how you are writing a test-cases. May be it would be useful to provide documentation (simple instruction) about it.Possible, then my experience will bring a little more benefits for you..
Author
Owner

@doctrinebot commented on GitHub (Dec 13, 2015):

Imported 1 attachments from Jira into https://gist.github.com/f049d09b217a5e59f4d5

@doctrinebot commented on GitHub (Dec 13, 2015): Imported 1 attachments from Jira into https://gist.github.com/f049d09b217a5e59f4d5 - [10402_DDC353Test.php](https://gist.github.com/f049d09b217a5e59f4d5#file-10402_DDC353Test-php)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#439