Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractCollection.php
Go to the documentation of this file.
1 <?php
7 
9 
14 {
20  protected $storeManager;
21 
25  protected $metadataPool;
26 
37  public function __construct(
38  \Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory,
39  \Psr\Log\LoggerInterface $logger,
40  \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy,
41  \Magento\Framework\Event\ManagerInterface $eventManager,
44  \Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
45  \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
46  ) {
47  $this->storeManager = $storeManager;
48  $this->metadataPool = $metadataPool;
49  parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
50  }
51 
59  protected function performAfterLoad($tableName, $linkField)
60  {
61  $linkedIds = $this->getColumnValues($linkField);
62  if (count($linkedIds)) {
63  $connection = $this->getConnection();
64  $select = $connection->select()->from(['cms_entity_store' => $this->getTable($tableName)])
65  ->where('cms_entity_store.' . $linkField . ' IN (?)', $linkedIds);
66  $result = $connection->fetchAll($select);
67  if ($result) {
68  $storesData = [];
69  foreach ($result as $storeData) {
70  $storesData[$storeData[$linkField]][] = $storeData['store_id'];
71  }
72 
73  foreach ($this as $item) {
74  $linkedId = $item->getData($linkField);
75  if (!isset($storesData[$linkedId])) {
76  continue;
77  }
78  $storeIdKey = array_search(Store::DEFAULT_STORE_ID, $storesData[$linkedId], true);
79  if ($storeIdKey !== false) {
80  $stores = $this->storeManager->getStores(false, true);
81  $storeId = current($stores)->getId();
82  $storeCode = key($stores);
83  } else {
84  $storeId = current($storesData[$linkedId]);
85  $storeCode = $this->storeManager->getStore($storeId)->getCode();
86  }
87  $item->setData('_first_store_id', $storeId);
88  $item->setData('store_code', $storeCode);
89  $item->setData('store_id', $storesData[$linkedId]);
90  }
91  }
92  }
93  }
94 
102  public function addFieldToFilter($field, $condition = null)
103  {
104  if ($field === 'store_id') {
105  return $this->addStoreFilter($condition, false);
106  }
107 
108  return parent::addFieldToFilter($field, $condition);
109  }
110 
118  abstract public function addStoreFilter($store, $withAdmin = true);
119 
127  protected function performAddStoreFilter($store, $withAdmin = true)
128  {
129  if ($store instanceof Store) {
130  $store = [$store->getId()];
131  }
132 
133  if (!is_array($store)) {
134  $store = [$store];
135  }
136 
137  if ($withAdmin) {
139  }
140 
141  $this->addFilter('store', ['in' => $store], 'public');
142  }
143 
151  protected function joinStoreRelationTable($tableName, $linkField)
152  {
153  if ($this->getFilter('store')) {
154  $this->getSelect()->join(
155  ['store_table' => $this->getTable($tableName)],
156  'main_table.' . $linkField . ' = store_table.' . $linkField,
157  []
158  )->group(
159  'main_table.' . $linkField
160  );
161  }
162  parent::_renderFiltersBefore();
163  }
164 
172  public function getSelectCountSql()
173  {
174  $countSelect = parent::getSelectCountSql();
175  $countSelect->reset(\Magento\Framework\DB\Select::GROUP);
176 
177  return $countSelect;
178  }
179 
186  public function toOptionIdArray()
187  {
188  $res = [];
189  $existingIdentifiers = [];
190  foreach ($this as $item) {
191  $identifier = $item->getData('identifier');
192 
193  $data['value'] = $identifier;
194  $data['label'] = $item->getData('title');
195 
196  if (in_array($identifier, $existingIdentifiers)) {
197  $data['value'] .= '|' . $item->getData($this->getIdFieldName());
198  } else {
199  $existingIdentifiers[] = $identifier;
200  }
201 
202  $res[] = $data;
203  }
204 
205  return $res;
206  }
207 }
$tableName
Definition: trigger.php:13
$resource
Definition: bulk.php:12
$logger
addFilter($field, $value, $type='and')
Definition: Collection.php:118
$storeCode
Definition: indexer.php:15
__construct(\Magento\Framework\Data\Collection\EntityFactoryInterface $entityFactory, \Psr\Log\LoggerInterface $logger, \Magento\Framework\Data\Collection\Db\FetchStrategyInterface $fetchStrategy, \Magento\Framework\Event\ManagerInterface $eventManager, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\EntityManager\MetadataPool $metadataPool, \Magento\Framework\DB\Adapter\AdapterInterface $connection=null, \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource=null)
const GROUP
Definition: Select.php:52
$connection
Definition: bulk.php:13