Query.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Search\Model\ResourceModel;
  7. use Magento\Framework\DB\Select;
  8. use Magento\Framework\Model\AbstractModel;
  9. use Magento\Framework\Model\ResourceModel\Db\AbstractDb;
  10. use Magento\Search\Model\Query as QueryModel;
  11. /**
  12. * Search query resource model
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Query extends AbstractDb
  17. {
  18. /**
  19. * Date
  20. *
  21. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  22. */
  23. protected $_date;
  24. /**
  25. * @var \Magento\Framework\Stdlib\DateTime
  26. */
  27. protected $dateTime;
  28. /**
  29. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  30. * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
  31. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  32. * @param string $connectionName
  33. */
  34. public function __construct(
  35. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  36. \Magento\Framework\Stdlib\DateTime\DateTime $date,
  37. \Magento\Framework\Stdlib\DateTime $dateTime,
  38. $connectionName = null
  39. ) {
  40. $this->_date = $date;
  41. $this->dateTime = $dateTime;
  42. parent::__construct($context, $connectionName);
  43. }
  44. /**
  45. * Custom load model only by query text (skip synonym for)
  46. *
  47. * @param AbstractModel $object
  48. * @param string $value
  49. * @return $this
  50. */
  51. public function loadByQueryText(AbstractModel $object, $value)
  52. {
  53. $select = $this->getConnection()->select()->from(
  54. $this->getMainTable()
  55. )->where(
  56. 'query_text = ?',
  57. $value
  58. )->where(
  59. 'store_id = ?',
  60. $object->getStoreId()
  61. )->limit(
  62. 1
  63. );
  64. $data = $this->getConnection()->fetchRow($select);
  65. if ($data) {
  66. $object->setData($data);
  67. $this->_afterLoad($object);
  68. }
  69. return $this;
  70. }
  71. /**
  72. * Loading string as a value or regular numeric
  73. *
  74. * @param AbstractModel $object
  75. * @param int|string $value
  76. * @param null|string $field
  77. * @return $this|\Magento\Framework\Model\ResourceModel\Db\AbstractDb
  78. * @SuppressWarnings("unused")
  79. */
  80. public function load(AbstractModel $object, $value, $field = null)
  81. {
  82. if (is_numeric($value)) {
  83. return parent::load($object, $value);
  84. } else {
  85. $this->loadByQueryText($object, $value);
  86. }
  87. return $this;
  88. }
  89. /**
  90. * Custom load model by search query string
  91. *
  92. * @param AbstractModel $object
  93. * @param string $value
  94. * @return $this
  95. * @deprecated 100.1.0 "synonym for" feature has been removed
  96. */
  97. public function loadByQuery(AbstractModel $object, $value)
  98. {
  99. $this->loadByQueryText($object, $value);
  100. return $this;
  101. }
  102. /**
  103. * @param AbstractModel $object
  104. * @return $this
  105. */
  106. public function _beforeSave(AbstractModel $object)
  107. {
  108. $object->setUpdatedAt($this->dateTime->formatDate($this->_date->gmtTimestamp()));
  109. return $this;
  110. }
  111. /**
  112. * Init resource data
  113. *
  114. * @return void
  115. */
  116. protected function _construct()
  117. {
  118. $this->_init('search_query', 'query_id');
  119. }
  120. /**
  121. * Save query with incremental popularity
  122. *
  123. * @param QueryModel $query
  124. * @return void
  125. *
  126. * @throws \Magento\Framework\Exception\LocalizedException
  127. */
  128. public function saveIncrementalPopularity(QueryModel $query)
  129. {
  130. $adapter = $this->getConnection();
  131. $table = $this->getMainTable();
  132. $saveData = [
  133. 'store_id' => $query->getStoreId(),
  134. 'query_text' => $query->getQueryText(),
  135. 'popularity' => 1,
  136. ];
  137. $updateData = [
  138. 'popularity' => new \Zend_Db_Expr('`popularity` + 1'),
  139. ];
  140. $adapter->insertOnDuplicate($table, $saveData, $updateData);
  141. }
  142. /**
  143. * Save query with number of results
  144. *
  145. * @param QueryModel $query
  146. * @return void
  147. *
  148. * @throws \Magento\Framework\Exception\LocalizedException
  149. */
  150. public function saveNumResults(QueryModel $query)
  151. {
  152. $adapter = $this->getConnection();
  153. $table = $this->getMainTable();
  154. $numResults = $query->getNumResults();
  155. $saveData = [
  156. 'store_id' => $query->getStoreId(),
  157. 'query_text' => $query->getQueryText(),
  158. 'num_results' => $numResults,
  159. ];
  160. $updateData = ['num_results' => $numResults];
  161. $adapter->insertOnDuplicate($table, $saveData, $updateData);
  162. }
  163. }