123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Search\Model\ResourceModel;
- use Magento\Framework\DB\Select;
- use Magento\Framework\Model\AbstractModel;
- use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
- use Magento\Search\Model\Query as QueryModel;
- /**
- * Search query resource model
- * @api
- * @since 100.0.2
- */
- class Query extends AbstractDb
- {
- /**
- * Date
- *
- * @var \Magento\Framework\Stdlib\DateTime\DateTime
- */
- protected $_date;
- /**
- * @var \Magento\Framework\Stdlib\DateTime
- */
- protected $dateTime;
- /**
- * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
- * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
- * @param \Magento\Framework\Stdlib\DateTime $dateTime
- * @param string $connectionName
- */
- public function __construct(
- \Magento\Framework\Model\ResourceModel\Db\Context $context,
- \Magento\Framework\Stdlib\DateTime\DateTime $date,
- \Magento\Framework\Stdlib\DateTime $dateTime,
- $connectionName = null
- ) {
- $this->_date = $date;
- $this->dateTime = $dateTime;
- parent::__construct($context, $connectionName);
- }
- /**
- * Custom load model only by query text (skip synonym for)
- *
- * @param AbstractModel $object
- * @param string $value
- * @return $this
- */
- public function loadByQueryText(AbstractModel $object, $value)
- {
- $select = $this->getConnection()->select()->from(
- $this->getMainTable()
- )->where(
- 'query_text = ?',
- $value
- )->where(
- 'store_id = ?',
- $object->getStoreId()
- )->limit(
- 1
- );
- $data = $this->getConnection()->fetchRow($select);
- if ($data) {
- $object->setData($data);
- $this->_afterLoad($object);
- }
- return $this;
- }
- /**
- * Loading string as a value or regular numeric
- *
- * @param AbstractModel $object
- * @param int|string $value
- * @param null|string $field
- * @return $this|\Magento\Framework\Model\ResourceModel\Db\AbstractDb
- * @SuppressWarnings("unused")
- */
- public function load(AbstractModel $object, $value, $field = null)
- {
- if (is_numeric($value)) {
- return parent::load($object, $value);
- } else {
- $this->loadByQueryText($object, $value);
- }
- return $this;
- }
- /**
- * Custom load model by search query string
- *
- * @param AbstractModel $object
- * @param string $value
- * @return $this
- * @deprecated 100.1.0 "synonym for" feature has been removed
- */
- public function loadByQuery(AbstractModel $object, $value)
- {
- $this->loadByQueryText($object, $value);
- return $this;
- }
- /**
- * @param AbstractModel $object
- * @return $this
- */
- public function _beforeSave(AbstractModel $object)
- {
- $object->setUpdatedAt($this->dateTime->formatDate($this->_date->gmtTimestamp()));
- return $this;
- }
- /**
- * Init resource data
- *
- * @return void
- */
- protected function _construct()
- {
- $this->_init('search_query', 'query_id');
- }
- /**
- * Save query with incremental popularity
- *
- * @param QueryModel $query
- * @return void
- *
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function saveIncrementalPopularity(QueryModel $query)
- {
- $adapter = $this->getConnection();
- $table = $this->getMainTable();
- $saveData = [
- 'store_id' => $query->getStoreId(),
- 'query_text' => $query->getQueryText(),
- 'popularity' => 1,
- ];
- $updateData = [
- 'popularity' => new \Zend_Db_Expr('`popularity` + 1'),
- ];
- $adapter->insertOnDuplicate($table, $saveData, $updateData);
- }
- /**
- * Save query with number of results
- *
- * @param QueryModel $query
- * @return void
- *
- * @throws \Magento\Framework\Exception\LocalizedException
- */
- public function saveNumResults(QueryModel $query)
- {
- $adapter = $this->getConnection();
- $table = $this->getMainTable();
- $numResults = $query->getNumResults();
- $saveData = [
- 'store_id' => $query->getStoreId(),
- 'query_text' => $query->getQueryText(),
- 'num_results' => $numResults,
- ];
- $updateData = ['num_results' => $numResults];
- $adapter->insertOnDuplicate($table, $saveData, $updateData);
- }
- }
|