简体   繁体   English

Sonata/Symfony 使用 MongoDB 过滤 Null

[英]Sonata/Symfony using MongoDB filtering for Null

I'm trying to customize a filter in sonata admin that returns all the objects that have null on one specific field... In MySQL, there was a function that I used to get the null values which were the is null.我正在尝试在 Sonata admin 中自定义一个过滤器,该过滤器返回在一个特定字段上具有 null 的所有对象......在 MySQL 中,有一个函数用于获取 null 值,即为 null。 I'm trying to do the same but using mongo this time it gives an error that the isnull function is not defined.我正在尝试做同样的事情,但是这次使用 mongo 会给出一个错误,即未定义 isnull 函数。 Is there any function similar to that in the form to use in odm? odm中使用的形式有没有类似的功能? My code is the following:我的代码如下:

->add("isRoot", 'doctrine_mongo_callback', array(
                'callback' => function ($queryBuilder, $alias, $field, $value) {
                        /**
                         * @var QueryBuilder $queryBuilder
                         */
                        if ($value['value']) {
                            if ($value['value'] == 0) {
                                $queryBuilder->andWhere($queryBuilder->expr()->isNull($alias.'.mainCategory'));
                                return true;
                            } else {
                               $category = $this->getConfigurationPool()->getContainer()->get('doctrine_mongodb.odm.document_manager')->getReference(ArticleCategory::class, $value['value']);
                               $queryBuilder->andWhere($queryBuilder->expr()->eq($alias.'.category', $category));
                                return true;
                            }
                        }
                    },
                'field_type' => ChoiceType::class,
                'field_options' => array(
                    'choices' => $this->getCategoryChoices()        
                ),
                'label' => 'mainCategory'
            ));
        }

Is there a function in ODM similar to isNull in ORM? ODM 中是否有类似于 ORM 中的 isNull 的功能? the following is the error: Attempted to call an undefined method named "isNull" of class "Doctrine\\ODM\\MongoDB\\Query\\Expr".以下是错误:尝试调用类“Doctrine\\ODM\\MongoDB\\Query\\Expr”的名为“isNull”的未定义方法。

So I figured how to query instead of所以我想出了如何查询而不是

$queryBuilder->andWhere($queryBuilder->expr()->isNull($alias.'.mainCategory'));

Using the following: $queryBuilder->field('mainCategory')->equals(null);使用以下内容: $queryBuilder->field('mainCategory')->equals(null);

I saw this solution on the following link: enter link description here我在以下链接上看到了这个解决方案: 在此处输入链接描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM