1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File: Collection.php |
4
|
|
|
* |
5
|
|
|
* @author Maciej Sławik <[email protected]> |
6
|
|
|
* Github: https://github.com/maciejslawik |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace MSlwk\ICatalogue\Model\ResourceModel\Catalogue; |
10
|
|
|
|
11
|
|
|
use Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection; |
12
|
|
|
use Magento\Store\Model\Store; |
13
|
|
|
use MSlwk\ICatalogue\Model\ResourceModel\Catalogue as CatalogueResource; |
14
|
|
|
use MSlwk\ICatalogue\Api\CatalogueInterface; |
15
|
|
|
use MSlwk\ICatalogue\Api\Catalogue\StoreInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Collection |
19
|
|
|
* |
20
|
|
|
* @package MSlwk\ICatalogue\Model\ResourceModel\Catalogue |
21
|
|
|
*/ |
22
|
|
|
class Collection extends AbstractCollection |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @inheritdoc |
26
|
|
|
*/ |
27
|
|
|
protected function _construct() |
28
|
|
|
{ |
29
|
|
|
$this->_init('MSlwk\ICatalogue\Model\Catalogue', 'MSlwk\ICatalogue\Model\ResourceModel\Catalogue'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
|
|
protected function _afterLoad() |
36
|
|
|
{ |
37
|
|
|
$this->loadStoresData(CatalogueResource::CATALOGUE_STORE_TABLE, CatalogueInterface::CATALOGUE_ID); |
38
|
|
|
return parent::_afterLoad(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param $tableName |
43
|
|
|
* @param $linkField |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
protected function loadStoresData($tableName, $linkField) |
47
|
|
|
{ |
48
|
|
|
$connection = $this->getConnection(); |
49
|
|
|
$select = $connection->select()->from($tableName); |
50
|
|
|
$result = $connection->fetchAll($select); |
51
|
|
|
if ($result) { |
52
|
|
|
$storesData = []; |
53
|
|
|
foreach ($result as $storeData) { |
54
|
|
|
$storesData[$storeData[$linkField]][] = $storeData[StoreInterface::STORE_ID]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
foreach ($this as $item) { |
58
|
|
|
$linkedId = $item->getData($linkField); |
59
|
|
|
if (!isset($storesData[$linkedId])) { |
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
$item->setStores($storesData[$linkedId]); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
|
|
public function addFieldToFilter($field, $condition = null) |
71
|
|
|
{ |
72
|
|
|
if ($field === 'stores') { |
73
|
|
|
return $this->addStoreFilter($condition, false); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return parent::addFieldToFilter($field, $condition); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Add filter by store |
81
|
|
|
* |
82
|
|
|
* @param int|array|Store $store |
83
|
|
|
* @param bool $withAdmin |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function addStoreFilter($store, $withAdmin = true) |
87
|
|
|
{ |
88
|
|
|
if ($store instanceof Store) { |
|
|
|
|
89
|
|
|
$store = [$store->getId()]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (!is_array($store)) { |
93
|
|
|
$store = [$store]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if ($withAdmin) { |
97
|
|
|
$store[] = Store::DEFAULT_STORE_ID; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$this->addFilter(StoreInterface::STORE_ID, ['in' => $store]); |
101
|
|
|
return $this; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritdoc |
106
|
|
|
*/ |
107
|
|
|
protected function _renderFiltersBefore() |
108
|
|
|
{ |
109
|
|
|
if ($this->getFilter(StoreInterface::STORE_ID)) { |
110
|
|
|
$this->joinStoreRelationTable(); |
111
|
|
|
} |
112
|
|
|
return parent::_renderFiltersBefore(); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Join store relation table |
117
|
|
|
* |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
protected function joinStoreRelationTable() |
121
|
|
|
{ |
122
|
|
|
$this->getSelect()->join( |
123
|
|
|
['store_table' => CatalogueResource::CATALOGUE_STORE_TABLE], |
124
|
|
|
'main_table.' . CatalogueInterface::CATALOGUE_ID . ' = store_table.' . CatalogueInterface::CATALOGUE_ID, |
125
|
|
|
[] |
126
|
|
|
)->group( |
127
|
|
|
'main_table.' . CatalogueInterface::CATALOGUE_ID |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.