Fulltext.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogSearch\Model\ResourceModel;
  7. use Magento\Catalog\Api\Data\ProductInterface;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\EntityManager\MetadataPool;
  10. /**
  11. * CatalogSearch Fulltext Index resource model
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Fulltext extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  17. {
  18. /**
  19. * Core event manager proxy
  20. *
  21. * @var \Magento\Framework\Event\ManagerInterface
  22. */
  23. protected $_eventManager;
  24. /**
  25. * Holder for MetadataPool instance.
  26. *
  27. * @var MetadataPool
  28. */
  29. private $metadataPool;
  30. /**
  31. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  32. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  33. * @param string $connectionName
  34. * @param MetadataPool $metadataPool
  35. */
  36. public function __construct(
  37. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  38. \Magento\Framework\Event\ManagerInterface $eventManager,
  39. $connectionName = null,
  40. MetadataPool $metadataPool = null
  41. ) {
  42. $this->_eventManager = $eventManager;
  43. $this->metadataPool = $metadataPool ? : ObjectManager::getInstance()->get(MetadataPool::class);
  44. parent::__construct($context, $connectionName);
  45. }
  46. /**
  47. * Init resource model
  48. *
  49. * @return void
  50. */
  51. protected function _construct()
  52. {
  53. $this->_init('catalogsearch_fulltext', 'product_id');
  54. }
  55. /**
  56. * Reset search results
  57. *
  58. * @return $this
  59. * @deprecated 101.0.0 Not used anymore
  60. * @see Fulltext::resetSearchResultsByStore
  61. */
  62. public function resetSearchResults()
  63. {
  64. $connection = $this->getConnection();
  65. $connection->update($this->getTable('search_query'), ['is_processed' => 0], ['is_processed != 0']);
  66. $this->_eventManager->dispatch('catalogsearch_reset_search_result');
  67. return $this;
  68. }
  69. /**
  70. * Reset search results by store
  71. *
  72. * @param int $storeId
  73. * @return $this
  74. * @since 101.0.0
  75. */
  76. public function resetSearchResultsByStore($storeId)
  77. {
  78. $storeId = (int) $storeId;
  79. $connection = $this->getConnection();
  80. $connection->update(
  81. $this->getTable('search_query'),
  82. ['is_processed' => 0],
  83. ['is_processed != ?' => 0, 'store_id = ?' => $storeId]
  84. );
  85. $this->_eventManager->dispatch('catalogsearch_reset_search_result', ['store_id' => $storeId]);
  86. return $this;
  87. }
  88. /**
  89. * Retrieve product relations by children.
  90. *
  91. * @param int|array $childIds
  92. * @return array
  93. * @since 100.2.0
  94. */
  95. public function getRelationsByChild($childIds)
  96. {
  97. $connection = $this->getConnection();
  98. $linkField = $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField();
  99. $select = $connection
  100. ->select()
  101. ->from(
  102. ['relation' => $this->getTable('catalog_product_relation')],
  103. []
  104. )->distinct(true)
  105. ->join(
  106. ['cpe' => $this->getTable('catalog_product_entity')],
  107. 'cpe.' . $linkField . ' = relation.parent_id',
  108. ['cpe.entity_id']
  109. )->where(
  110. 'relation.child_id IN (?)',
  111. $childIds
  112. );
  113. return $connection->fetchCol($select);
  114. }
  115. }