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 
9 
17 {
23  protected $_storeId;
24 
30  protected $_storeManager;
31 
37  protected $_resourceHelper;
38 
49  public function __construct(
50  \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
51  \Psr\Log\LoggerInterface $logger,
52  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
53  \Magento\Framework\Event\ManagerInterface $eventManager,
55  \Magento\Framework\DB\Helper $resourceHelper,
56  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
57  \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
58  ) {
59  $this->_storeManager = $storeManager;
60  $this->_resourceHelper = $resourceHelper;
61  parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
62  }
63 
69  protected function _construct()
70  {
71  $this->_init(\Magento\Search\Model\Query::class, \Magento\Search\Model\ResourceModel\Query::class);
72  }
73 
80  public function setStoreId($store)
81  {
82  if ($store instanceof Store) {
83  $store = $store->getId();
84  }
85  $this->_storeId = $store;
86  return $this;
87  }
88 
94  public function getStoreId()
95  {
96  return $this->_storeId;
97  }
98 
105  public function setQueryFilter($query)
106  {
107  $this->getSelect()->reset(
108  \Magento\Framework\DB\Select::FROM
109  )->distinct(
110  true
111  )->from(
112  ['main_table' => $this->getTable('search_query')]
113  )->where(
114  'num_results > 0 AND display_in_terms = 1 AND query_text LIKE ?',
115  $this->_resourceHelper->addLikeEscape($query, ['position' => 'start'])
116  )->order(
117  'popularity ' . \Magento\Framework\DB\Select::SQL_DESC
118  );
119  if ($this->getStoreId()) {
120  $this->getSelect()->where('store_id = ?', (int)$this->getStoreId());
121  }
122  return $this;
123  }
124 
131  public function setPopularQueryFilter($storeIds = null)
132  {
133 
134  $this->getSelect()->reset(
135  \Magento\Framework\DB\Select::FROM
136  )->reset(
137  \Magento\Framework\DB\Select::COLUMNS
138  )->distinct(
139  true
140  )->from(
141  ['main_table' => $this->getTable('search_query')]
142  );
143  if ($storeIds) {
144  $this->addStoreFilter($storeIds);
145  $this->getSelect()->where('num_results > 0');
146  } elseif (null === $storeIds) {
147  $this->addStoreFilter($this->_storeManager->getStore()->getId());
148  $this->getSelect()->where('num_results > 0');
149  }
150 
151  $this->getSelect()->order(['popularity desc']);
152 
153  return $this;
154  }
155 
161  public function setRecentQueryFilter()
162  {
163  $this->setOrder('updated_at', 'desc');
164  return $this;
165  }
166 
173  public function addStoreFilter($storeIds)
174  {
175  if (!is_array($storeIds)) {
176  $storeIds = [$storeIds];
177  }
178  $this->getSelect()->where('main_table.store_id IN (?)', $storeIds);
179  return $this;
180  }
181 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$storeManager
$resource
Definition: bulk.php:12
$logger
const FROM
Definition: Select.php:49
__construct(\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\DB\Helper $resourceHelper, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource=null)
Definition: Collection.php:49
const COLUMNS
Definition: Select.php:48
const SQL_DESC
Definition: Select.php:82
setOrder($field, $direction=self::SORT_ORDER_DESC)
Definition: AbstractDb.php:274
$connection
Definition: bulk.php:13