Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Option.php
Go to the documentation of this file.
1 <?php
7 
12 
17 {
21  private $validator;
22 
26  private $metadataPool;
27 
31  private $entityManager;
32 
39  public function __construct(
40  \Magento\Framework\Model\ResourceModel\Db\Context $context,
41  \Magento\Bundle\Model\Option\Validator $validator,
42  $connectionName = null,
43  EntityManager $entityManager = null
44  ) {
45  parent::__construct($context, $connectionName);
46  $this->validator = $validator;
47 
48  $this->entityManager = $entityManager
49  ?: ObjectManager::getInstance()->get(EntityManager::class);
50  }
51 
57  protected function _construct()
58  {
59  $this->_init('catalog_product_bundle_option', 'option_id');
60  }
61 
67  {
68  return $this->getConnection()->delete(
69  $this->getTable('catalog_product_bundle_selection'),
70  ['option_id =?' => $optionId]
71  );
72  }
73 
80  protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
81  {
82  parent::_afterSave($object);
83 
84  $condition = [
85  'option_id = ?' => $object->getId(),
86  'store_id = ? OR store_id = 0' => $object->getStoreId(),
87  'parent_product_id = ?' => $object->getParentId()
88  ];
89 
90  $connection = $this->getConnection();
91  $connection->delete($this->getTable('catalog_product_bundle_option_value'), $condition);
92 
93  $data = new \Magento\Framework\DataObject();
94  $data->setOptionId($object->getId())
95  ->setStoreId($object->getStoreId())
96  ->setParentProductId($object->getParentId())
97  ->setTitle($object->getTitle());
98 
99  $connection->insert($this->getTable('catalog_product_bundle_option_value'), $data->getData());
100 
104  if (0 !== (int)$object->getStoreId()) {
105  $data->setStoreId(0)->setTitle($object->getDefaultTitle());
106  $connection->insert($this->getTable('catalog_product_bundle_option_value'), $data->getData());
107  }
108 
109  return $this;
110  }
111 
118  protected function _afterDelete(\Magento\Framework\Model\AbstractModel $object)
119  {
120  parent::_afterDelete($object);
121 
122  $this->getConnection()
123  ->delete(
124  $this->getTable('catalog_product_bundle_option_value'),
125  [
126  'option_id = ?' => $object->getId(),
127  'parent_product_id = ?' => $object->getParentId()
128  ]
129  );
130 
131  return $this;
132  }
133 
142  {
143  $connection = $this->getConnection();
144 
145  $title = $connection->getCheckSql(
146  'option_title_store.title IS NOT NULL',
147  'option_title_store.title',
148  'option_title_default.title'
149  );
150  $bind = ['store_id' => $storeId, 'product_id' => $productId];
151  $linkField = $this->getMetadataPool()->getMetadata(ProductInterface::class)->getLinkField();
152  $select = $connection->select()
153  ->from(
154  ['opt' => $this->getMainTable()],
155  []
156  )
157  ->join(
158  ['option_title_default' => $this->getTable('catalog_product_bundle_option_value')],
159  'option_title_default.option_id = opt.option_id AND option_title_default.store_id = 0',
160  []
161  )
162  ->joinLeft(
163  ['option_title_store' => $this->getTable('catalog_product_bundle_option_value')],
164  'option_title_store.option_id = opt.option_id AND option_title_store.store_id = :store_id',
165  ['title' => $title]
166  )
167  ->join(
168  ['e' => $this->getTable('catalog_product_entity')],
169  "e.$linkField = opt.parent_id",
170  []
171  )
172  ->where(
173  'e.entity_id=:product_id'
174  );
175  if (!($searchData = $connection->fetchCol($select, $bind))) {
176  $searchData = [];
177  }
178 
179  return $searchData;
180  }
181 
186  {
187  return $this->validator;
188  }
189 
194  private function getMetadataPool()
195  {
196  if (!$this->metadataPool) {
197  $this->metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
198  }
199  return $this->metadataPool;
200  }
201 
205  public function save(\Magento\Framework\Model\AbstractModel $object)
206  {
207  $this->entityManager->save($object);
208 
209  return $this;
210  }
211 }
$title
Definition: default.phtml:14
_afterDelete(\Magento\Framework\Model\AbstractModel $object)
Definition: Option.php:118
save(\Magento\Framework\Model\AbstractModel $object)
Definition: Option.php:205
_afterSave(\Magento\Framework\Model\AbstractModel $object)
Definition: Option.php:80
__construct(\Magento\Framework\Model\ResourceModel\Db\Context $context, \Magento\Bundle\Model\Option\Validator $validator, $connectionName=null, EntityManager $entityManager=null)
Definition: Option.php:39
$connection
Definition: bulk.php:13
getSearchableData($productId, $storeId)
Definition: Option.php:141