Sonata/Symfony using MongoDB filtering for Null

I’m trying to customize a filter in sonata admin that return 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 was 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 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'
            ));
        }

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *