Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Query.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Search\Model;
7 
9 use Magento\Search\Model\ResourceModel\Query\CollectionFactory as QueryCollectionFactory;
11 use Magento\Search\Model\SearchCollectionFactory as CollectionFactory;
18 
44 class Query extends AbstractModel implements QueryInterface
45 {
51  protected $_eventPrefix = 'search_query';
52 
58  protected $_eventObject = 'search_query';
59 
60  const CACHE_TAG = 'SEARCH_QUERY';
61 
62  const XML_PATH_MIN_QUERY_LENGTH = 'catalog/search/min_query_length';
63 
64  const XML_PATH_MAX_QUERY_LENGTH = 'catalog/search/max_query_length';
65 
71  protected $_scopeConfig;
72 
78  protected $_storeManager;
79 
86 
93 
107  public function __construct(
108  \Magento\Framework\Model\Context $context,
110  QueryCollectionFactory $queryCollectionFactory,
111  CollectionFactory $searchCollectionFactory,
113  ScopeConfigInterface $scopeConfig,
115  DbCollection $resourceCollection = null,
116  array $data = []
117  ) {
118  $this->_queryCollectionFactory = $queryCollectionFactory;
119  $this->_searchCollectionFactory = $searchCollectionFactory;
120  $this->_storeManager = $storeManager;
121  $this->_scopeConfig = $scopeConfig;
122  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
123  }
124 
130  protected function _construct()
131  {
132  $this->_init(\Magento\Search\Model\ResourceModel\Query::class);
133  }
134 
140  public function getSearchCollection()
141  {
142  return $this->_searchCollectionFactory->create();
143  }
144 
151  public function getSuggestCollection()
152  {
153  $collection = $this->getData('suggest_collection');
154  if ($collection === null) {
155  $collection = $this->_queryCollectionFactory->create()->setStoreId(
156  $this->getStoreId()
157  )->setQueryFilter(
158  $this->getQueryText()
159  );
160  $this->setData('suggest_collection', $collection);
161  }
162  return $collection;
163  }
164 
173  public function loadByQuery($text)
174  {
175  $this->loadByQueryText($text);
176  return $this;
177  }
178 
186  public function loadByQueryText($text)
187  {
188  $this->_getResource()->loadByQueryText($this, $text);
189  $this->_afterLoad();
190  $this->setOrigData();
191  return $this;
192  }
193 
200  public function setStoreId($storeId)
201  {
202  $this->setData('store_id', $storeId);
203  }
204 
211  public function getStoreId()
212  {
213  if (!($storeId = $this->getData('store_id'))) {
214  $storeId = $this->_storeManager->getStore()->getId();
215  }
216  return $storeId;
217  }
218 
225  public function prepare()
226  {
227  if (!$this->getId()) {
228  $this->setIsActive(0);
229  $this->setIsProcessed(0);
230  $this->save();
231  $this->setIsActive(1);
232  }
233 
234  return $this;
235  }
236 
244  public function saveIncrementalPopularity()
245  {
246  $this->getResource()->saveIncrementalPopularity($this);
247 
248  return $this;
249  }
250 
259  public function saveNumResults($numResults)
260  {
261  $this->setNumResults($numResults);
262  $this->getResource()->saveNumResults($this);
263 
264  return $this;
265  }
266 
273  public function getMinQueryLength()
274  {
275  return $this->_scopeConfig->getValue(
276  self::XML_PATH_MIN_QUERY_LENGTH,
277  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
278  $this->getStoreId()
279  );
280  }
281 
288  public function getMaxQueryLength()
289  {
290  return $this->_scopeConfig->getValue(
291  self::XML_PATH_MAX_QUERY_LENGTH,
292  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
293  $this->getStoreId()
294  );
295  }
296 
300  public function getQueryText()
301  {
302  return $this->getDataByKey('query_text');
303  }
304 
311  public function isQueryTextExceeded()
312  {
313  return $this->getData('is_query_text_exceeded');
314  }
315 
323  public function isQueryTextShort()
324  {
325  return $this->getData('is_query_text_short');
326  }
327 }
getData($key='', $index=null)
Definition: DataObject.php:119
const XML_PATH_MIN_QUERY_LENGTH
Definition: Query.php:62
$storeManager
endifif( $block->getLastPageNum()>1)( 'Page') ?></strong >< ul class $text
Definition: pager.phtml:43
$resource
Definition: bulk.php:12
const XML_PATH_MAX_QUERY_LENGTH
Definition: Query.php:64
saveNumResults($numResults)
Definition: Query.php:259
__construct(\Magento\Framework\Model\Context $context, Registry $registry, QueryCollectionFactory $queryCollectionFactory, CollectionFactory $searchCollectionFactory, StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig, AbstractResource $resource=null, DbCollection $resourceCollection=null, array $data=[])
Definition: Query.php:107