mongodbsymfonycallbackdoctrinesonata

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. I'm trying to do the same but using mongo this time it gives an error that the isnull function is not defined. Is there any function similar to that in the form to use in 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? the following is the error: Attempted to call an undefined method named "isNull" of class "Doctrine\ODM\MongoDB\Query\Expr".


Solution

  • So I figured how to query instead of

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

    Using the following: $queryBuilder->field('mainCategory')->equals(null);

    I saw this solution on the following link: enter link description here