Skip to content

Commit 7afc993

Browse files
damcouJan Petr
authored andcommitted
Adding "total" to adapter queries (introduced by Magento 2.3) (#791)
1 parent 87c4183 commit 7afc993

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

Adapter/Algolia.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use AlgoliaSearch\AlgoliaConnectionException;
77
use Magento\Framework\App\ResourceConnection;
88
use Magento\Framework\DB\Ddl\Table;
9+
use Magento\Framework\DB\Select;
910
use Magento\Framework\Search\Adapter\Mysql\Aggregation\Builder as AggregationBuilder;
1011
use Magento\Framework\Search\Adapter\Mysql\DocumentFactory;
1112
use Magento\Framework\Search\Adapter\Mysql\Mapper;
@@ -40,6 +41,11 @@ class Algolia implements AdapterInterface
4041
/** @var DocumentFactory */
4142
private $documentFactory;
4243

44+
private $countSqlSkipParts = [
45+
\Magento\Framework\DB\Select::LIMIT_COUNT => true,
46+
\Magento\Framework\DB\Select::LIMIT_OFFSET => true,
47+
];
48+
4349
/**
4450
* @param Mapper $mapper
4551
* @param ResponseFactory $responseFactory
@@ -103,6 +109,7 @@ public function query(RequestInterface $request)
103109
$response = [
104110
'documents' => $documents,
105111
'aggregations' => $aggregations,
112+
'total' => 0,
106113
];
107114

108115
return $this->responseFactory->create($response);
@@ -120,6 +127,7 @@ private function nativeQuery(RequestInterface $request)
120127
$response = [
121128
'documents' => $documents,
122129
'aggregations' => $aggregations,
130+
'total' => $this->getSize($query),
123131
];
124132

125133
return $this->responseFactory->create($response);
@@ -130,6 +138,29 @@ private function getApiDocument($document)
130138
return $this->documentFactory->create($document);
131139
}
132140

141+
private function getSize(Select $query)
142+
{
143+
$sql = $this->getSelectCountSql($query);
144+
$parentSelect = $this->getConnection()->select();
145+
$parentSelect->from(['core_select' => $sql]);
146+
$parentSelect->reset(\Magento\Framework\DB\Select::COLUMNS);
147+
$parentSelect->columns('COUNT(*)');
148+
$totalRecords = $this->getConnection()->fetchOne($parentSelect);
149+
150+
return intval($totalRecords);
151+
}
152+
153+
private function getSelectCountSql(Select $query)
154+
{
155+
foreach ($this->countSqlSkipParts as $part => $toSkip) {
156+
if ($toSkip) {
157+
$query->reset($part);
158+
}
159+
}
160+
161+
return $query;
162+
}
163+
133164
/**
134165
* Executes query and return raw response
135166
*

Model/Observer/CatalogPermissions/ProductCollectionAddPermissions.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ protected function addProductPermissionsData($additionalData, $productIds, $stor
6565
foreach ($permissions as $permission) {
6666
list($permissionStoreId, $customerGroupId, $level) = explode('_', $permission);
6767
if ($permissionStoreId == $storeId) {
68-
6968
$additionalData->addProductData($productId, [
7069
'customer_group_permission_' . $customerGroupId => (($level == -2 || $level != -1
7170
&& !$catalogPermissionsHelper->isAllowedCategoryView()) ? 0 : 1),

0 commit comments

Comments
 (0)