Sonata mongodb admin bundle filter by reference

Posted

技术标签:

【中文标题】Sonata mongodb admin bundle filter by reference【英文标题】: 【发布时间】:2016-10-11 09:06:36 【问题描述】:

我有文件

/** @MongoDB\ReferenceMany(targetDocument="NewsTag") */ private $tags;

关系。

在我的奏鸣曲管理员中,我想按标签过滤列表。我尝试使用

$datagridMapper->add('tags', null, array(), null, array('expanded' => true, 'multiple' => true))

在文档中,但有一个错误

Could not load type "document"

有人知道为什么吗?

UPD:堆栈跟踪

Symfony\Component\Form\Exception\InvalidArgumentException: Could not load type "document"
at n/a
    in /var/www/meteoprog-site-s3/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php line 87

at Symfony\Component\Form\FormRegistry->getType('document')
    in /var/www/meteoprog-site-s3/vendor/symfony/symfony/src/Symfony/Component/Form/FormFactory.php line 83

at Symfony\Component\Form\FormFactory->createNamedBuilder('value', 'document', null, array('required' => false, 'class' => 'AppBundle\Document\NewsTag', 'expanded' => true, 'multiple' => true))
    in /var/www/meteoprog-site-s3/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 106

at Symfony\Component\Form\FormBuilder->create('value', 'document', array('required' => false, 'class' => 'AppBundle\Document\NewsTag', 'expanded' => true, 'multiple' => true))
    in /var/www/meteoprog-site-s3/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 267

at Symfony\Component\Form\FormBuilder->resolveChildren()
    in /var/www/meteoprog-site-s3/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 215

at Symfony\Component\Form\FormBuilder->getForm()
    in /var/www/meteoprog-site-s3/vendor/symfony/symfony/src/Symfony/Component/Form/FormBuilder.php line 221

at Symfony\Component\Form\FormBuilder->getForm()
    in /var/www/meteoprog-site-s3/var/cache/dev/classes.php line 11085

at Sonata\AdminBundle\Datagrid\Datagrid->buildPager()
    in /var/www/meteoprog-site-s3/var/cache/dev/classes.php line 11189

at Sonata\AdminBundle\Datagrid\Datagrid->getForm()
    in /var/www/meteoprog-site-s3/vendor/sonata-project/admin-bundle/Controller/CRUDController.php line 105

at Sonata\AdminBundle\Controller\CRUDController->listAction()
    in  line 

at call_user_func_array(array(object(LanguagesAdminController), 'listAction'), array())
    in /var/www/meteoprog-site-s3/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 139

at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), '1')
    in /var/www/meteoprog-site-s3/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/HttpKernel.php line 62

at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), '1', true)
    in /var/www/meteoprog-site-s3/vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Kernel.php line 169

at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
    in /var/www/meteoprog-site-s3/web/app_dev.php line 30

【问题讨论】:

我认为第二个参数null 被猜测为“文档”。尝试将其替换为 dacument 以确认。如果错误消息是关于dacument,我的理论是正确的。此外,您应该提供堆栈跟踪(作为文本,请参阅页面底部) 是的。它猜测为document。我已经在源代码中找到了位置。问题是第二个论点应该是什么。我试过doctrine_mongo_model,但也猜成document/ 第四个参数呢?我总是把它们混在一起…… 好的,现在我有另一个错误。我尝试$datagridMapper->add('tags', null, [], 'sonata_type_model', ['multiple' => true, 'property' => 'name', 'by_reference' => false]),错误是Catchable Fatal Error: Argument 1 passed to Sonata\AdminBundle\Form\ChoiceList\ModelChoiceLoader::__construct() must implement interface Sonata\AdminBundle\Model\ModelManagerInterface, null given, called in /var/www/meteoprog-site-s3/var/cache/dev/classes.php on line 13150 and defined 【参考方案1】:

因此,如果您想使用 sonata_type_model 进行过滤,则可能会出现错误。您在 ChoiceField 中没有 modelManager。 但是您可以像这样在 configureDatagridFilters 中手动添加它:

   $datagridMapper
        ->add('tags', null, [], 'sonata_type_model',
            [
                'model_manager' => $datagridMapper->getAdmin()->getModelManager(), 
                'property' => 'name', 
                'multiple' => true, 
                'by_reference' => false
            ]);

【讨论】:

以上是关于Sonata mongodb admin bundle filter by reference的主要内容,如果未能解决你的问题,请参考以下文章

SonataDoctrineORMAdminExtension.php

Sonata admin 基于 ODM 的子管理员在基于 ORM 的管理员上

当sonata_type_admin调用时如何在Sonata的Admin类中获取底层对象?

Sonata 管理包:无法删除与 sonata_type_admin 的关系

服务“admin.category”依赖于不存在的服务“sonata.admin.manager.orm”

有没有办法确定 Sonata\AdminBundle\Admin\Admin::configureFormFields() 中的当前操作(创建或编辑)?