Skip to content

Commit 0e2a4ad

Browse files
committed
Merge pull request #354 from magento-firedrakes/MAGETWO-39255
[Firedrakes] Sprint 57
2 parents ecf466f + 27e439b commit 0e2a4ad

File tree

21 files changed

+293
-38
lines changed

21 files changed

+293
-38
lines changed

app/code/Magento/Catalog/Model/CategoryRepository.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Magento\Framework\Exception\CouldNotSaveException;
1111
use Magento\Framework\Exception\NoSuchEntityException;
1212
use Magento\Framework\Exception\StateException;
13+
use Magento\Catalog\Api\Data\CategoryInterface;
1314

1415
class CategoryRepository implements \Magento\Catalog\Api\CategoryRepositoryInterface
1516
{
@@ -33,6 +34,11 @@ class CategoryRepository implements \Magento\Catalog\Api\CategoryRepositoryInter
3334
*/
3435
protected $categoryResource;
3536

37+
/**
38+
* @var \Magento\Framework\Model\Entity\MetadataPool
39+
*/
40+
protected $metadataPool;
41+
3642
/**
3743
* List of fields that can used config values in case when value does not defined directly
3844
*
@@ -44,15 +50,18 @@ class CategoryRepository implements \Magento\Catalog\Api\CategoryRepositoryInter
4450
* @param \Magento\Catalog\Model\CategoryFactory $categoryFactory
4551
* @param \Magento\Catalog\Model\ResourceModel\Category $categoryResource
4652
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
53+
* @param \Magento\Framework\Model\Entity\MetadataPool $metadataPool
4754
*/
4855
public function __construct(
4956
\Magento\Catalog\Model\CategoryFactory $categoryFactory,
5057
\Magento\Catalog\Model\ResourceModel\Category $categoryResource,
51-
\Magento\Store\Model\StoreManagerInterface $storeManager
58+
\Magento\Store\Model\StoreManagerInterface $storeManager,
59+
\Magento\Framework\Model\Entity\MetadataPool $metadataPool
5260
) {
5361
$this->categoryFactory = $categoryFactory;
5462
$this->categoryResource = $categoryResource;
5563
$this->storeManager = $storeManager;
64+
$this->metadataPool = $metadataPool;
5665
}
5766

5867
/**
@@ -61,10 +70,21 @@ public function __construct(
6170
public function save(\Magento\Catalog\Api\Data\CategoryInterface $category)
6271
{
6372
$existingData = $category->toFlatArray();
73+
6474
/** 'available_sort_by' should be set separately because fields of array type are destroyed by toFlatArray() */
6575
$existingData['available_sort_by'] = $category->getAvailableSortBy();
76+
6677
if ($category->getId()) {
78+
$metadata = $this->metadataPool->getMetadata(
79+
CategoryInterface::class
80+
);
81+
6782
$existingCategory = $this->get($category->getId());
83+
84+
$existingData[$metadata->getLinkField()] = $existingCategory->getData(
85+
$metadata->getLinkField()
86+
);
87+
6888
if (isset($existingData['image']) && is_array($existingData['image'])) {
6989
$existingData['image_additional_data'] = $existingData['image'];
7090
unset($existingData['image']);

app/code/Magento/Catalog/Model/Indexer/Product/Flat/FlatTableBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
/**
1313
* Class FlatTableBuilder
14+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1415
*/
1516
class FlatTableBuilder
1617
{
@@ -279,7 +280,7 @@ protected function _fillTemporaryFlatTable(array $tables, $storeId, $valueFieldS
279280
if (!empty($columnValueNames)) {
280281
$select->joinLeft(
281282
$temporaryValueTableName,
282-
"e.${linkField} = " . $temporaryValueTableName . ".${linkField}",
283+
"e.${linkField} = " . $temporaryValueTableName . ".entity_id",
283284
$columnValueNames
284285
);
285286
$allColumns = array_merge($allColumns, $columnValueNames);

app/code/Magento/Catalog/Model/Indexer/Product/Price/AbstractAction.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,11 +378,11 @@ protected function _reindexRows($changedIds = [])
378378
if (!empty($notCompositeIds)) {
379379
$select = $this->_connection->select()->from(
380380
['l' => $this->_defaultIndexerResource->getTable('catalog_product_relation')],
381-
'parent_id'
381+
''
382382
)->join(
383383
['e' => $this->_defaultIndexerResource->getTable('catalog_product_entity')],
384-
'e.entity_id = l.parent_id',
385-
['type_id']
384+
'e.' . $this->getProductIdFieldName() . ' = l.parent_id',
385+
['e.entity_id as parent_id', 'type_id']
386386
)->where(
387387
'l.child_id IN(?)',
388388
$notCompositeIds
@@ -422,11 +422,15 @@ protected function _reindexRows($changedIds = [])
422422
*/
423423
protected function _copyRelationIndexData($parentIds, $excludeIds = null)
424424
{
425+
$linkField = $this->getProductIdFieldName();
425426
$select = $this->_connection->select()->from(
426427
$this->_defaultIndexerResource->getTable('catalog_product_relation'),
427428
['child_id']
429+
)->join(
430+
['e' => $this->_defaultIndexerResource->getTable('catalog_product_entity')],
431+
'e.' . $linkField . ' = parent_id'
428432
)->where(
429-
'parent_id IN(?)',
433+
'e.entity_id IN(?)',
430434
$parentIds
431435
);
432436
if (!empty($excludeIds)) {

app/code/Magento/Catalog/Model/ResourceModel/Product/Collection.php

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,9 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
100100
protected $_addTaxPercents = false;
101101

102102
/**
103-
* Product limitation filters
104-
* Allowed filters
105-
* store_id int;
106-
* category_id int;
107-
* category_is_anchor int;
108-
* visibility array|int;
109-
* website_ids array|int;
110-
* store_table string;
111-
* use_price_index bool; join price index table flag
112-
* customer_group_id int; required for price; customer group limitation for price
113-
* website_id int; required for price; website limitation for price
114-
*
115-
* @var array
103+
* @var \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation
116104
*/
117-
protected $_productLimitationFilters = [];
105+
protected $_productLimitationFilters;
118106

119107
/**
120108
* Category product count select
@@ -275,6 +263,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Collection\Abstrac
275263
* @param \Magento\Customer\Model\Session $customerSession
276264
* @param \Magento\Framework\Stdlib\DateTime $dateTime
277265
* @param GroupManagementInterface $groupManagement
266+
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation
278267
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
279268
*
280269
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
@@ -299,6 +288,7 @@ public function __construct(
299288
\Magento\Customer\Model\Session $customerSession,
300289
\Magento\Framework\Stdlib\DateTime $dateTime,
301290
GroupManagementInterface $groupManagement,
291+
\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation,
302292
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null
303293
) {
304294
$this->moduleManager = $moduleManager;
@@ -311,6 +301,7 @@ public function __construct(
311301
$this->_resourceHelper = $resourceHelper;
312302
$this->dateTime = $dateTime;
313303
$this->_groupManagement = $groupManagement;
304+
$this->_productLimitationFilters = $productLimitation;
314305
parent::__construct(
315306
$entityFactory,
316307
$logger,
@@ -811,7 +802,7 @@ public function addWebsiteFilter($websites = null)
811802
/**
812803
* Get filters applied to collection
813804
*
814-
* @return array
805+
* @return \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation
815806
*/
816807
public function getLimitationFilters()
817808
{
@@ -1434,7 +1425,7 @@ public function setAllIdsCache($value)
14341425
*/
14351426
public function addPriceData($customerGroupId = null, $websiteId = null)
14361427
{
1437-
$this->_productLimitationFilters['use_price_index'] = true;
1428+
$this->_productLimitationFilters->setUsePriceIndex(true);
14381429

14391430
if (!isset($this->_productLimitationFilters['customer_group_id']) && is_null($customerGroupId)) {
14401431
$customerGroupId = $this->_customerSession->getCustomerGroupId();
@@ -1646,9 +1637,10 @@ public function addAttributeToSort($attribute, $dir = self::SORT_ORDER_ASC)
16461637
$storeId = $this->getStoreId();
16471638
if ($attribute == 'price' && $storeId != 0) {
16481639
$this->addPriceData();
1649-
$this->getSelect()->order("price_index.min_price {$dir}");
1650-
1651-
return $this;
1640+
if ($this->_productLimitationFilters->isUsingPriceIndex()) {
1641+
$this->getSelect()->order("price_index.min_price {$dir}");
1642+
return $this;
1643+
}
16521644
}
16531645

16541646
if ($this->isEnabledFlat()) {
@@ -1864,7 +1856,7 @@ protected function _productLimitationJoinPrice()
18641856
protected function _productLimitationPrice($joinLeft = false)
18651857
{
18661858
$filters = $this->_productLimitationFilters;
1867-
if (empty($filters['use_price_index'])) {
1859+
if (!$filters->isUsingPriceIndex()) {
18681860
return $this;
18691861
}
18701862

@@ -1923,7 +1915,7 @@ protected function _productLimitationPrice($joinLeft = false)
19231915
*/
19241916
public function applyFrontendPriceLimitations()
19251917
{
1926-
$this->_productLimitationFilters['use_price_index'] = true;
1918+
$this->_productLimitationFilters->setUsePriceIndex(true);
19271919
if (!isset($this->_productLimitationFilters['customer_group_id'])) {
19281920
$customerGroupId = $this->_customerSession->getCustomerGroupId();
19291921
$this->_productLimitationFilters['customer_group_id'] = $customerGroupId;
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Model\ResourceModel\Product\Collection;
7+
8+
/**
9+
* Class ProductLimitation
10+
*/
11+
class ProductLimitation implements \ArrayAccess
12+
{
13+
/**
14+
* Product limitation filters
15+
* Allowed filters
16+
* store_id int;
17+
* category_id int;
18+
* category_is_anchor int;
19+
* visibility array|int;
20+
* website_ids array|int;
21+
* store_table string;
22+
* use_price_index bool; join price index table flag
23+
* customer_group_id int; required for price; customer group limitation for price
24+
* website_id int; required for price; website limitation for price
25+
*
26+
* @var array
27+
*/
28+
private $productLimitationFilters = [];
29+
30+
/**
31+
* @param string $offset
32+
* @return bool
33+
*/
34+
public function offsetExists($offset)
35+
{
36+
return array_key_exists($offset, $this->productLimitationFilters);
37+
}
38+
39+
/**
40+
* @param string $offset
41+
* @return mixed
42+
*/
43+
public function offsetGet($offset)
44+
{
45+
return $this->productLimitationFilters[$offset];
46+
}
47+
48+
/**
49+
* @param string $offset
50+
* @param mixed $value
51+
* @return void
52+
*/
53+
public function offsetSet($offset, $value)
54+
{
55+
$this->productLimitationFilters[$offset] = $value;
56+
}
57+
58+
/**
59+
* @param string $offset
60+
* @return void
61+
*/
62+
public function offsetUnset($offset)
63+
{
64+
unset($this->productLimitationFilters[$offset]);
65+
}
66+
67+
/**
68+
* @return int|null
69+
*/
70+
public function getStoreId()
71+
{
72+
return $this->offsetGet('store_id');
73+
}
74+
75+
/**
76+
* @return int|null
77+
*/
78+
public function getCategoryId()
79+
{
80+
return $this->offsetGet('category_id');
81+
}
82+
83+
/**
84+
* @return int|null
85+
*/
86+
public function getCategoryIsAnchor()
87+
{
88+
return $this->offsetGet('category_is_anchor');
89+
}
90+
91+
/**
92+
* @return array|int|null
93+
*/
94+
public function getVisibility()
95+
{
96+
return $this->offsetGet('visibility');
97+
}
98+
99+
/**
100+
* @return array|int|null
101+
*/
102+
public function getWebsiteIds()
103+
{
104+
return $this->offsetGet('website_ids');
105+
}
106+
107+
/**
108+
* @return string|null
109+
*/
110+
public function getStoreTable()
111+
{
112+
return $this->offsetGet('store_table');
113+
}
114+
115+
/**
116+
* Join price index table flag
117+
*
118+
* @return bool
119+
*/
120+
public function isUsingPriceIndex()
121+
{
122+
return $this->offsetExists('use_price_index') ? (bool)$this->offsetGet('use_price_index') : false;
123+
}
124+
125+
/**
126+
* @param bool $value
127+
* @return void
128+
*/
129+
public function setUsePriceIndex($value)
130+
{
131+
$this->offsetSet('use_price_index', (bool)$value);
132+
}
133+
134+
/**
135+
* Required for price; customer group limitation for price
136+
*
137+
* @return int|null
138+
*/
139+
public function getCustomerGroupId()
140+
{
141+
return $this->offsetGet('customer_group_id');
142+
}
143+
144+
/**
145+
* Required for price; website limitation for price
146+
*
147+
* @return int|null
148+
*/
149+
public function getWebsiteId()
150+
{
151+
return $this->offsetGet('website_id');
152+
}
153+
}

app/code/Magento/Catalog/Model/ResourceModel/Product/Compare/Item/Collection.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class Collection extends \Magento\Catalog\Model\ResourceModel\Product\Collection
7070
* @param \Magento\Customer\Model\Session $customerSession
7171
* @param \Magento\Framework\Stdlib\DateTime $dateTime
7272
* @param \Magento\Customer\Api\GroupManagementInterface $groupManagement
73+
* @param \Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation
7374
* @param \Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem
7475
* @param \Magento\Catalog\Helper\Product\Compare $catalogProductCompare
7576
* @param \Magento\Framework\DB\Adapter\AdapterInterface $connection
@@ -96,6 +97,7 @@ public function __construct(
9697
\Magento\Customer\Model\Session $customerSession,
9798
\Magento\Framework\Stdlib\DateTime $dateTime,
9899
\Magento\Customer\Api\GroupManagementInterface $groupManagement,
100+
\Magento\Catalog\Model\ResourceModel\Product\Collection\ProductLimitation $productLimitation,
99101
\Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem,
100102
\Magento\Catalog\Helper\Product\Compare $catalogProductCompare,
101103
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null
@@ -122,6 +124,7 @@ public function __construct(
122124
$customerSession,
123125
$dateTime,
124126
$groupManagement,
127+
$productLimitation,
125128
$connection
126129
);
127130
}

0 commit comments

Comments
 (0)