flow-development-collection icon indicating copy to clipboard operation
flow-development-collection copied to clipboard

BUGFIX: isNewObject

Open kitsunet opened this issue 3 years ago • 6 comments

With this change PersistenceManager::isNewObject will correctly report object status in all circumstances.

Fixes: #2699

kitsunet avatar Feb 10 '22 08:02 kitsunet

Yes definitely!

kitsunet avatar Feb 10 '22 08:02 kitsunet

This will report "false" on unmanaged new Entities. Shouldn't we take the extra mile and provide the call with a custom state like "UNKNOWN" and try to see if the entity is fetchable from persistence to replace guessing with knowledge?

fcool avatar Feb 10 '22 08:02 fcool

Without assumption doctrine will look into the DB, and it will only ever come back with DETACHED or NEW, so that seems alright to me?

kitsunet avatar Feb 10 '22 10:02 kitsunet

Oh? Ok... obiously it is easy to make false assumptions. ;) I would never have thought, that Doctrine would report DETACHED for unknown entities, but well... fine then.

fcool avatar Feb 10 '22 10:02 fcool

From my understanding of the code it will report DETACHED if the entity is found in the DB but was not in the unit of work (which seems the correct info then) and otherwise it would report NEW, which would also seem correct.

I am actually more concerned about the test failures because the seem rather unrelated? I will have a look locally....

kitsunet avatar Feb 10 '22 12:02 kitsunet

Only the FormObjectsTest has errors.

kdambekalns avatar Mar 30 '22 20:03 kdambekalns

These test failures actually expose a problem in our ValueObject implementation (in combination with embedded=true I think). We never went beyond the first 5 lines of \Doctrine\ORM\UnitOfWork::getEntityState due to the $assume parameter. Now UoW tries to call \Doctrine\ORM\Mapping\ClassMetadataInfo::getIdentifierValues() on the Location ValueObject used as Fixture in these tests. That results in an exception that is hidden away due to the functinoal test and using browser requests:

Uncaught Exception in Flow #1: Warning: Undefined array key 0 in /Volumes/CaseSensitive/Work/PhpstormProjects/neos-dev-73/Packages/Libraries/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php line 940
thrown in file Packages/Framework/Neos.Flow/Classes/Error/ErrorHandler.php
in line 81

65 Neos\Flow\Error\ErrorHandler::handleError(2, "Undefined array key 0", "/Volumes/CaseSensitive/Work/PhpstormProjects/neos-…orm/lib/Doctrine/ORM/Mapping/ClassMetadataInfo.php", 940)
64 Doctrine\ORM\Mapping\ClassMetadataInfo::getIdentifierValues(Neos\FluidAdaptor\Tests\Functional\Form\Fixtures\Domain\Model\Location)
63 Doctrine\ORM\UnitOfWork::getEntityState(Neos\FluidAdaptor\Tests\Functional\Form\Fixtures\Domain\Model\Location)
62 Neos\Flow\Persistence\Doctrine\PersistenceManager_Original::isNewObject(Neos\FluidAdaptor\Tests\Functional\Form\Fixtures\Domain\Model\Location)
61 Neos\FluidAdaptor\ViewHelpers\Form\AbstractFormViewHelper::renderHiddenIdentityField(Neos\FluidAdaptor\Tests\Functional\Form\Fixtures\Domain\Model\Location, "post[author][location]")
60 Neos\FluidAdaptor\ViewHelpers\Form\AbstractFormFieldViewHelper::addAdditionalIdentityPropertiesIfNeeded()
59 Neos\FluidAdaptor\ViewHelpers\Form\TextfieldViewHelper_Original::render()

I assume ValueObjects never come by said method usually. I wonder if we should catch them as special case in isNewObject because we can't even know the state for an embedded ValueObject like this...

kitsunet avatar Feb 13 '23 10:02 kitsunet

I find \Neos\FluidAdaptor\ViewHelpers\Form\AbstractFormViewHelper::renderHiddenIdentityField a bit "loose" in that it just asks for isNewObject on any object, regardless if we know if it's even a ORM managed object. BUT I am also unclear how we can detect one from the other. Figuring out if a class is annotated ValueObject(embedded=true) seems rather cumbersome.

kitsunet avatar Feb 13 '23 11:02 kitsunet

Well... every PersistenceObject should get the PersistenceMagicAspet introduced, if I understand it correctly.

That way we should be able to ask it, what kind of object it is, shouldn't we?

fcool avatar Feb 13 '23 11:02 fcool

Well... every PersistenceObject should get the PersistenceMagicAspet introduced, if I understand it correctly.

That way we should be able to ask it, what kind of object it is, shouldn't we?

facepalm, good call, that should do the trick. I'll try.

kitsunet avatar Feb 13 '23 11:02 kitsunet

I guess we should technically type hint PersistenceMagicInterface for the methods in the PersistenceManager, would be breaking but maybe something for 9.0 ?

kitsunet avatar Feb 13 '23 13:02 kitsunet

Psalm errors are unrelated, so this seems good.

kitsunet avatar Feb 13 '23 14:02 kitsunet