Skip to content

Commit 9cce090

Browse files
authored
MongoDBAdapter: set filtered and total rows in prepareQuery (#375)
1 parent 96a3d64 commit 9cce090

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/Adapter/MongoDB/MongoDBAdapter.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
use MongoDB\BSON\UTCDateTime;
1616
use MongoDB\Collection;
17+
use MongoDB\Driver\CursorInterface;
1718
use MongoDB\Model\BSONDocument;
1819
use Omines\DataTablesBundle\Adapter\AbstractAdapter;
1920
use Omines\DataTablesBundle\Adapter\AdapterQuery;
@@ -56,7 +57,17 @@ protected function prepareQuery(AdapterQuery $query): void
5657
}
5758
}
5859

60+
$state = $query->getState();
61+
62+
$filter = $this->buildFilter($state);
63+
$options = $this->buildOptions($state);
64+
5965
$query->setTotalRows($this->collection->count());
66+
$query->setFilteredRows($this->collection->count($filter));
67+
$cursor = $this->collection->find($filter, $options);
68+
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
69+
70+
$query->set('cursor', $cursor);
6071
}
6172

6273
protected function mapPropertyPath(AdapterQuery $query, AbstractColumn $column): ?string
@@ -69,14 +80,8 @@ protected function mapPropertyPath(AdapterQuery $query, AbstractColumn $column):
6980
*/
7081
protected function getResults(AdapterQuery $query): \Traversable
7182
{
72-
$state = $query->getState();
73-
74-
$filter = $this->buildFilter($state);
75-
$options = $this->buildOptions($state);
76-
77-
$query->setFilteredRows($this->collection->count($filter));
78-
$cursor = $this->collection->find($filter, $options);
79-
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
83+
/** @var CursorInterface $cursor */
84+
$cursor = $query->get('cursor');
8085

8186
/** @var BSONDocument $result */
8287
foreach ($cursor as $result) {

0 commit comments

Comments
 (0)