I don't know if there is already a fix for this or not, but I can't seem to find a solution.
If I have a reference in my DB, and its parent document is removed (but the referenced document remains), it crashes the whole app with a fatal error:
Uncaught exception 'Doctrine\ODM\MongoDB\DocumentNotFoundException' with message 'The "Proxies\__CG__\Documents\Post" document with identifier "5543dcd66a9c6cf8100041a8" could not be found.
In my app I have a "social feed" with posts aggregated from several social networks. The admin has the ability to blacklist a post, which creates a new document in the Blacklisted collection with a reference back to the original post. But if the original post is deleted without the blacklist being deleted as well, this error shows up. I do have cascading options on the reference so it will delete the blacklist when the post is deleted, but I want to be sure this error won't cause any problems in the future. Is there a way to ignore this error, delete any references without a parent, or set the reference to null if it doesn't exist?
The thing I'm using right now is something like this:
class PricingBase
{
public function doesReferencedItemExist()
{
try {
if ($this->reference instanceof Proxy) {
$this->reference->__load();
} elseif ($this->reference === null) {
return false;
}
} catch (DocumentNotFoundException $e) {
return false;
}
return true;
}
public function getReferencedItem()
{
if ($this->doesReferencedItemExist() === false) {
$this->reference = null;
}
return $this->reference;
}
}
Also there's a PR #532 but it needs some work before being merged.
@jdmaurer feel free to reopen the issue if you have any doubts or comments
Hi,
I'm really interested about this subject, @malarzm how did you define this PricingBase class? And how did you registered it on Doctrine?
@jeremyb in my case PricingBase is MappedSuperclass because I'm later extending it by concrete Pricing mapped as @Document and CartPricing mapped as @EmbeddedDocument, but that's not relevant. Important part is having doesReferencedItemExist() and calling it before getting referenced object to get actual object or null in case the referenced object doesn't exist without getting exception
Ok @malarzm, I think I get the idea. I'll try that. Thanks! :)
Most helpful comment
The thing I'm using right now is something like this:
Also there's a PR #532 but it needs some work before being merged.