php – 当在Doctrine MongoDB中删除该对象时,如何删除对象的任何引用?

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – 当在Doctrine MongoDB中删除该对象时,如何删除对象的任何引用?脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我的页面上出现以下严重错误

The "WildkatProxy\DocumentsTagPRoxy" document wITh identifier "4e90eede17bc2ec68c000001" Could not be found.@H_304_8@ 
 

我假设这是因为文档标记代理已从其他操作中删除,因此引用不再有效.我现在想要删除对这个对象的陈旧引用,但是要静. (因为这可能会在很多其他情况下发生)无论如何,当删除对象时,我是否可以对引用已删除对象的所有对象执行“反向级联”?

完成此任务的最佳做法是什么

谢谢
安迪

@H_403_21@解决方法
我假设您有两个集合,ArticleTag和Article,其中的文章引用了文章标签.如果要在删除标记时从文章删除标记引用,则可以实现事件侦听器.

创建一个类:
    

namespace Foo\BarBundle\EventListener;

use Doctrine\ODM\MongoDB\Event\LifecycleEventargs;
use Foo\BarBundle\Document\Article;

class ArticleTagRemovalListener
{
    public function preRemove(LifecycleEventArgs $args)
    {
        $document = $args->getDocument();

        if ($document instanceof Article) {
            // Remove tag From all articles
            $args
                ->getDocumentManager()
                ->getRepository('FooBarBundle:Article')
                ->removeTag($document);
        }
    }
}

并在services.yMLXMl文件注册此类:

foo_bar.listener.tag_removal:
    class: Foo\BarBundle\EventListener\ArticleTagRemovalListener
    tags:
      - { name: doctrine_mongodb.odm.event_listener,event: preRemove }

接下来在文章自定义repository class添加以下方法

public function removeTag($tag)
{
    return $this
        ->createQueryBuilder()
        ->update()
        ->field('tags')->pull($tag)
        ->;multiple(true)
        ->getQuery()
        ->execute();
}

这将删除所有可用文章中的标记,然后再删除它.如果要将删除操作级联到所有文章文档. (因此,删除标记删除具有特定标记的所有文章,请使用以下存储库方法.)

public function purgeByTag($tag)
{
    $result = $this
        ->createQueryBuilder()
        ->remove()
        ->field('tags')->equals($tag)
        ->getQuery()
        ->execute();

    return $result['n'];
}

更新ArticleTagRemovalListener以调用方法并完成!

脚本宝典总结

以上是脚本宝典为你收集整理的php – 当在Doctrine MongoDB中删除该对象时,如何删除对象的任何引用?全部内容,希望文章能够帮你解决php – 当在Doctrine MongoDB中删除该对象时,如何删除对象的任何引用?所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。