Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
7 
15 {
21  protected $_reviewTable = null;
22 
28  protected $_reviewDetailTable = null;
29 
35  protected $_reviewStatusTable = null;
36 
42  protected $_reviewEntityTable = null;
43 
49  protected $_reviewStoreTable = null;
50 
55  protected $_addStoreDataFlag = false;
56 
62  protected $_reviewData = null;
63 
69  protected $_voteFactory;
70 
76  protected $_storeManager;
77 
89  public function __construct(
90  \Magento\Framework\Data\Collection\EntityFactory $entityFactory,
91  \Psr\Log\LoggerInterface $logger,
92  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
93  \Magento\Framework\Event\ManagerInterface $eventManager,
94  \Magento\Review\Helper\Data $reviewData,
95  \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory,
96  \Magento\Store\Model\StoreManagerInterface $storeManager,
97  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
98  \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
99  ) {
100  $this->_reviewData = $reviewData;
101  $this->_voteFactory = $voteFactory;
102  $this->_storeManager = $storeManager;
103 
104  parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
105  }
106 
112  protected function _construct()
113  {
114  $this->_init(\Magento\Review\Model\Review::class, \Magento\Review\Model\ResourceModel\Review::class);
115  }
116 
122  protected function _initSelect()
123  {
124  parent::_initSelect();
125  $this->getSelect()->join(
126  ['detail' => $this->getReviewDetailTable()],
127  'main_table.review_id = detail.review_id',
128  ['detail_id', 'title', 'detail', 'nickname', 'customer_id']
129  );
130  return $this;
131  }
132 
140  {
141  $this->addFilter('customer', $this->getConnection()->quoteInto('detail.customer_id=?', $customerId), 'string');
142  return $this;
143  }
144 
151  public function addStoreFilter($storeId)
152  {
153  $inCond = $this->getConnection()->prepareSqlCondition('store.store_id', ['in' => $storeId]);
154  $this->getSelect()->join(
155  ['store' => $this->getReviewStoreTable()],
156  'main_table.review_id=store.review_id',
157  []
158  );
159  $this->getSelect()->where($inCond);
160  return $this;
161  }
162 
168  public function addStoreData()
169  {
170  $this->_addStoreDataFlag = true;
171  return $this;
172  }
173 
181  public function addEntityFilter($entity, $pkValue)
182  {
183  $reviewEntityTable = $this->getReviewEntityTable();
184  if (is_numeric($entity)) {
185  $this->addFilter('entity', $this->getConnection()->quoteInto('main_table.entity_id=?', $entity), 'string');
186  } elseif (is_string($entity)) {
187  $this->_select->join(
188  $reviewEntityTable,
189  'main_table.entity_id=' . $reviewEntityTable . '.entity_id',
190  ['entity_code']
191  );
192 
193  $this->addFilter(
194  'entity',
195  $this->getConnection()->quoteInto($reviewEntityTable . '.entity_code=?', $entity),
196  'string'
197  );
198  }
199 
200  $this->addFilter(
201  'entity_pk_value',
202  $this->getConnection()->quoteInto('main_table.entity_pk_value=?', $pkValue),
203  'string'
204  );
205 
206  return $this;
207  }
208 
215  public function addStatusFilter($status)
216  {
217  if (is_string($status)) {
218  $statuses = array_flip($this->_reviewData->getReviewStatuses());
219  $status = isset($statuses[$status]) ? $statuses[$status] : 0;
220  }
221  if (is_numeric($status)) {
222  $this->addFilter('status', $this->getConnection()->quoteInto('main_table.status_id=?', $status), 'string');
223  }
224  return $this;
225  }
226 
233  public function setDateOrder($dir = 'DESC')
234  {
235  $this->setOrder('main_table.created_at', $dir);
236  return $this;
237  }
238 
244  public function addRateVotes()
245  {
246  foreach ($this->getItems() as $item) {
247  $votesCollection = $this->_voteFactory->create()->getResourceCollection()->setReviewFilter(
248  $item->getId()
249  )->setStoreFilter(
250  $this->_storeManager->getStore()->getId()
251  )->addRatingInfo(
252  $this->_storeManager->getStore()->getId()
253  )->load();
254  $item->setRatingVotes($votesCollection);
255  }
256 
257  return $this;
258  }
259 
265  public function addReviewsTotalCount()
266  {
267  $this->_select->joinLeft(
268  ['r' => $this->getReviewTable()],
269  'main_table.entity_pk_value = r.entity_pk_value',
270  ['total_reviews' => new \Zend_Db_Expr('COUNT(r.review_id)')]
271  )->group(
272  'main_table.review_id'
273  );
274 
275  return $this;
276  }
277 
285  public function load($printQuery = false, $logQuery = false)
286  {
287  if ($this->isLoaded()) {
288  return $this;
289  }
290  $this->_eventManager->dispatch('review_review_collection_load_before', ['collection' => $this]);
291  parent::load($printQuery, $logQuery);
292  if ($this->_addStoreDataFlag) {
293  $this->_addStoreData();
294  }
295  return $this;
296  }
297 
303  protected function _addStoreData()
304  {
305  $connection = $this->getConnection();
306 
307  $reviewsIds = $this->getColumnValues('review_id');
308  $storesToReviews = [];
309  if (count($reviewsIds) > 0) {
310  $inCond = $connection->prepareSqlCondition('review_id', ['in' => $reviewsIds]);
311  $select = $connection->select()->from($this->getReviewStoreTable())->where($inCond);
312  $result = $connection->fetchAll($select);
313  foreach ($result as $row) {
314  if (!isset($storesToReviews[$row['review_id']])) {
315  $storesToReviews[$row['review_id']] = [];
316  }
317  $storesToReviews[$row['review_id']][] = $row['store_id'];
318  }
319  }
320 
321  foreach ($this as $item) {
322  if (isset($storesToReviews[$item->getId()])) {
323  $item->setStores($storesToReviews[$item->getId()]);
324  } else {
325  $item->setStores([]);
326  }
327  }
328  }
329 
335  protected function getReviewTable()
336  {
337  if ($this->_reviewTable === null) {
338  $this->_reviewTable = $this->getTable('review');
339  }
340  return $this->_reviewTable;
341  }
342 
348  protected function getReviewDetailTable()
349  {
350  if ($this->_reviewDetailTable === null) {
351  $this->_reviewDetailTable = $this->getTable('review_detail');
352  }
354  }
355 
361  protected function getReviewStatusTable()
362  {
363  if ($this->_reviewStatusTable === null) {
364  $this->_reviewStatusTable = $this->getTable('review_status');
365  }
367  }
368 
374  protected function getReviewEntityTable()
375  {
376  if ($this->_reviewEntityTable === null) {
377  $this->_reviewEntityTable = $this->getTable('review_entity');
378  }
380  }
381 
387  protected function getReviewStoreTable()
388  {
389  if ($this->_reviewStoreTable === null) {
390  $this->_reviewStoreTable = $this->getTable('review_store');
391  }
393  }
394 }
load($printQuery=false, $logQuery=false)
Definition: Collection.php:285
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$storeManager
$resource
Definition: bulk.php:12
$logger
addFilter($field, $value, $type='and')
Definition: Collection.php:118
$status
Definition: order_status.php:8
$entity
Definition: element.phtml:22
setOrder($field, $direction=self::SORT_ORDER_DESC)
Definition: AbstractDb.php:274
$connection
Definition: bulk.php:13
__construct(\Magento\Framework\Data\Collection\EntityFactory $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Review\Helper\Data $reviewData, \Magento\Review\Model\Rating\Option\VoteFactory $voteFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource=null)
Definition: Collection.php:89