Option.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Bundle\Model\ResourceModel;
  7. use Magento\Catalog\Api\Data\ProductInterface;
  8. use Magento\Framework\EntityManager\MetadataPool;
  9. use Magento\Framework\App\ObjectManager;
  10. use Magento\Framework\EntityManager\EntityManager;
  11. /**
  12. * Bundle Option Resource Model
  13. */
  14. class Option extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  15. {
  16. /**
  17. * @var \Magento\Bundle\Model\Option\Validator
  18. */
  19. private $validator;
  20. /**
  21. * @var MetadataPool
  22. */
  23. private $metadataPool;
  24. /**
  25. * @var EntityManager
  26. */
  27. private $entityManager;
  28. /**
  29. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  30. * @param \Magento\Bundle\Model\Option\Validator $validator
  31. * @param string $connectionName
  32. * @param EntityManager|null $entityManager
  33. */
  34. public function __construct(
  35. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  36. \Magento\Bundle\Model\Option\Validator $validator,
  37. $connectionName = null,
  38. EntityManager $entityManager = null
  39. ) {
  40. parent::__construct($context, $connectionName);
  41. $this->validator = $validator;
  42. $this->entityManager = $entityManager
  43. ?: ObjectManager::getInstance()->get(EntityManager::class);
  44. }
  45. /**
  46. * Initialize connection and define resource
  47. *
  48. * @return void
  49. */
  50. protected function _construct()
  51. {
  52. $this->_init('catalog_product_bundle_option', 'option_id');
  53. }
  54. /**
  55. * @param int $optionId
  56. * @return int
  57. */
  58. public function removeOptionSelections($optionId)
  59. {
  60. return $this->getConnection()->delete(
  61. $this->getTable('catalog_product_bundle_selection'),
  62. ['option_id =?' => $optionId]
  63. );
  64. }
  65. /**
  66. * After save process
  67. *
  68. * @param \Magento\Framework\Model\AbstractModel $object
  69. * @return $this
  70. */
  71. protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
  72. {
  73. parent::_afterSave($object);
  74. $condition = [
  75. 'option_id = ?' => $object->getId(),
  76. 'store_id = ? OR store_id = 0' => $object->getStoreId(),
  77. 'parent_product_id = ?' => $object->getParentId()
  78. ];
  79. $connection = $this->getConnection();
  80. $connection->delete($this->getTable('catalog_product_bundle_option_value'), $condition);
  81. $data = new \Magento\Framework\DataObject();
  82. $data->setOptionId($object->getId())
  83. ->setStoreId($object->getStoreId())
  84. ->setParentProductId($object->getParentId())
  85. ->setTitle($object->getTitle());
  86. $connection->insert($this->getTable('catalog_product_bundle_option_value'), $data->getData());
  87. /**
  88. * also saving default fallback value
  89. */
  90. if (0 !== (int)$object->getStoreId()) {
  91. $data->setStoreId(0)->setTitle($object->getDefaultTitle());
  92. $connection->insert($this->getTable('catalog_product_bundle_option_value'), $data->getData());
  93. }
  94. return $this;
  95. }
  96. /**
  97. * After delete process
  98. *
  99. * @param \Magento\Framework\Model\AbstractModel $object
  100. * @return $this
  101. */
  102. protected function _afterDelete(\Magento\Framework\Model\AbstractModel $object)
  103. {
  104. parent::_afterDelete($object);
  105. $this->getConnection()
  106. ->delete(
  107. $this->getTable('catalog_product_bundle_option_value'),
  108. [
  109. 'option_id = ?' => $object->getId(),
  110. 'parent_product_id = ?' => $object->getParentId()
  111. ]
  112. );
  113. return $this;
  114. }
  115. /**
  116. * Retrieve options searchable data
  117. *
  118. * @param int $productId
  119. * @param int $storeId
  120. * @return array
  121. */
  122. public function getSearchableData($productId, $storeId)
  123. {
  124. $connection = $this->getConnection();
  125. $title = $connection->getCheckSql(
  126. 'option_title_store.title IS NOT NULL',
  127. 'option_title_store.title',
  128. 'option_title_default.title'
  129. );
  130. $bind = ['store_id' => $storeId, 'product_id' => $productId];
  131. $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
  132. $select = $connection->select()
  133. ->from(
  134. ['opt' => $this->getMainTable()],
  135. []
  136. )
  137. ->join(
  138. ['option_title_default' => $this->getTable('catalog_product_bundle_option_value')],
  139. 'option_title_default.option_id = opt.option_id AND option_title_default.store_id = 0',
  140. []
  141. )
  142. ->joinLeft(
  143. ['option_title_store' => $this->getTable('catalog_product_bundle_option_value')],
  144. 'option_title_store.option_id = opt.option_id AND option_title_store.store_id = :store_id',
  145. ['title' => $title]
  146. )
  147. ->join(
  148. ['e' => $this->getTable('catalog_product_entity')],
  149. "e.$linkField = opt.parent_id",
  150. []
  151. )
  152. ->where(
  153. 'e.entity_id=:product_id'
  154. );
  155. if (!($searchData = $connection->fetchCol($select, $bind))) {
  156. $searchData = [];
  157. }
  158. return $searchData;
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function getValidationRulesBeforeSave()
  164. {
  165. return $this->validator;
  166. }
  167. /**
  168. * Get MetadataPool instance
  169. * @return MetadataPool
  170. */
  171. private function getMetadataPool()
  172. {
  173. if (!$this->metadataPool) {
  174. $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
  175. }
  176. return $this->metadataPool;
  177. }
  178. /**
  179. * {@inheritdoc}
  180. */
  181. public function save(\Magento\Framework\Model\AbstractModel $object)
  182. {
  183. $this->entityManager->save($object);
  184. return $this;
  185. }
  186. }