doctrine / mongodb-odm

The Official PHP MongoDB ORM/ODM
https://www.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/
MIT License
1.09k stars 502 forks source link

Handle DocumentNotFoundExceptions more gracefully or turn it off #2034

Closed abirhoss closed 4 years ago

abirhoss commented 5 years ago

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

When mongo references become invalid, doctrine throws the DocumentNotFoundException from doctrine mongo-odm Caught exception: The "MongoDBODMProxies\__CG__\CrLib\Document\Image" document with identifier "0e2922a9-7dcd-4709-ad8e-9d36cd98f11d" could not be found. on every document that has that now invalid reference. There is a way to disable this exception by setting $eventArgs->disableException(); in the documentNotFound function (mongodb-odm/lib/Doctrine/ODM/MongoDB/Utility/LifecycleEventManager.php). The original function is shown below

     * @param mixed $id
     *
     * @return bool Returns whether the exceptionDisabled flag was set
     */
    public function documentNotFound(object $proxy, $id) : bool
    {
        $eventArgs = new DocumentNotFoundEventArgs($proxy, $this->dm, $id);
        $this->evm->dispatchEvent(Events::documentNotFound, $eventArgs);
        return $eventArgs->isExceptionDisabled();
    }

Is there a way we can enable the disableException flag for DocumentNotFoundEventArgs through a config setting or handle DocumentNotFoundExceptions more gracefully? We dont want to put try catch blocks every where

malarzm commented 5 years ago

@Abir-H thanks for the idea! Right now there is no way to specify the flag globally however you may have some luck with an event listener that will disable exceptions for all not found documents.

alcaeus commented 5 years ago

Thinking about this I'm not entirely sure how we'd go on about this: when the hydrator encounters a reference, it creates a proxy object and injects that into the mapped property. The proxy object is then initialised when a method call is made on the proxy object. However, at that time it's too late to generically handle the missing proxy: we can't return null and also shouldn't return an uninitialised object.

Looking at the PR where we introduced this behaviour (#1336), there's a comment that explains the problem in detail: https://github.com/doctrine/mongodb-odm/pull/1336#issuecomment-342582157.

I'll see if we can come up with a way to allow to change this in the mapping, but I'm not sure it's possible. One thing we need to do either way is to show an example in the cookbook about how to properly handle this event and deal with a missing proxy object.

stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in a week if no further activity occurs. Thank you for your contributions.

alcaeus commented 4 years ago

Closing here as there is currently no feasible way to handle this (see comment linked above). I'm not sure if I want to expose handling this kind of behaviour in the mapping.