datatables-bundle icon indicating copy to clipboard operation
datatables-bundle copied to clipboard

I added a filter in MongoDBAdapter so the search no longer works !

Open SlimArfaaoui opened this issue 4 years ago • 3 comments

error message : $or must be an array i can solve this problem when i change in MongoDBAdapter.

private function buildFilter(DataTableState $state): array { ++ $filter = []; -- $filter = $this->filters; if (!empty($globalSearch = $state->getGlobalSearch())) { foreach ($state->getDataTable()->getColumns() as $column) { if ($column->isGlobalSearchable()) { $filter[] = [$column->getField() => new \MongoDB\BSON\Regex($globalSearch, 'i')]; } } $filter = ['$or' => $filter]; } ++ $filter = array_merge($filter, $this->filters); return $filter; }

SlimArfaaoui avatar Aug 02 '21 11:08 SlimArfaaoui

I don't understand the issue like this, seems like this is in how you use Mongo, not our bundle.

curry684 avatar Sep 21 '21 08:09 curry684

Hello @curry684

Today, we encountered the same error. We had a dataTable using MongoDBAdapter and a filter entry (see code below). The search was not working, but with the code of @SlimArfaaoui it works.

I can't really figure out what is the good way to use the filter entry otherwise....

            ->createAdapter(MongoDBAdapter::class, [
                'collection' => $documentManager->getDocumentCollection(Page::class),
                'filters' => [
                    '$and' => [
                        ['type' => 'blog'],
                        ['status' => ['$ne' => 'archive']]
                    ]
                ]
            ])
`
``

Clorr avatar Sep 22 '21 09:09 Clorr

Hello @curry684

I just added a simple filter in options MongoDbAdapter like this : $options = [ 'collection' => $collection, 'filters' => ['action' => $action] ];

and when i use global search i have this error "$or must be an array".

So when i followed the error i found in your bundle you collect filters configurated with global search inside $or like this : "$or" => array:10 [ "action" => "test" 0 => array:1 [ "id" => MongoDB\BSON\Regex {#1302 +"pattern": "aa" +"flags": "i" } ]

but when i use my solution (i set filters configurated outside $or) i will have this output for $filters : "$or" => array:8 [ 0 => array:1 [ "id" => MongoDB\BSON\Regex {#927 +"pattern": "aa" +"flags": "i" } ] . ... ] "action" => "test" ]

SlimArfaaoui avatar Oct 01 '21 09:10 SlimArfaaoui

Stale issue message

github-actions[bot] avatar Nov 20 '22 22:11 github-actions[bot]