Skip to content

Commit 94e86ab

Browse files
Merge branch 'develop' of github.com:magento/magento2ce into bugfix
2 parents 6f95a41 + 3f2ae24 commit 94e86ab

File tree

9 files changed

+761
-49
lines changed

9 files changed

+761
-49
lines changed

app/code/Magento/CatalogRule/Model/Indexer/IndexBuilder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ public function reindexByIds(array $ids)
159159
$this->doReindexByIds($ids);
160160
} catch (\Exception $e) {
161161
$this->critical($e);
162-
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()), $e);
162+
throw new \Magento\Framework\Exception\LocalizedException(
163+
__("Catalog rule indexing failed. See details in exception log.")
164+
);
163165
}
164166
}
165167

@@ -193,7 +195,9 @@ public function reindexFull()
193195
$this->doReindexFull();
194196
} catch (\Exception $e) {
195197
$this->critical($e);
196-
throw new \Magento\Framework\Exception\LocalizedException(__($e->getMessage()), $e);
198+
throw new \Magento\Framework\Exception\LocalizedException(
199+
__("Catalog rule indexing failed. See details in exception log.")
200+
);
197201
}
198202
}
199203

app/code/Magento/Rule/Model/ResourceModel/Rule/Collection/AbstractCollection.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,18 @@ public function addWebsitesToResult($flag = null)
7474
*/
7575
public function addWebsiteFilter($websiteId)
7676
{
77-
$entityInfo = $this->_getAssociatedEntityInfo('website');
7877
if (!$this->getFlag('is_website_table_joined')) {
78+
$websiteIds = is_array($websiteId) ? $websiteId : [$websiteId];
79+
$entityInfo = $this->_getAssociatedEntityInfo('website');
7980
$this->setFlag('is_website_table_joined', true);
80-
if ($websiteId instanceof \Magento\Store\Model\Website) {
81-
$websiteId = $websiteId->getId();
81+
foreach ($websiteIds as $index => $website) {
82+
if ($website instanceof \Magento\Store\Model\Website) {
83+
$websiteIds[$index] = $website->getId();
84+
}
8285
}
8386
$this->getSelect()->join(
8487
['website' => $this->getTable($entityInfo['associations_table'])],
85-
$this->getConnection()->quoteInto('website.' . $entityInfo['entity_id_field'] . ' = ?', $websiteId)
88+
$this->getConnection()->quoteInto('website.' . $entityInfo['entity_id_field'] . ' IN (?)', $websiteIds)
8689
. ' AND main_table.' . $entityInfo['rule_id_field'] . ' = website.' . $entityInfo['rule_id_field'],
8790
[]
8891
);

app/code/Magento/Rule/Test/Unit/Model/ResourceModel/Rule/Collection/AbstractCollectionTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ class AbstractCollectionTest extends \PHPUnit_Framework_TestCase
4545
*/
4646
protected $_db;
4747

48+
/**
49+
* @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
50+
*/
51+
private $connectionMock;
52+
53+
/**
54+
* @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject
55+
*/
56+
private $selectMock;
57+
4858
protected function setUp()
4959
{
5060
$this->_entityFactoryMock = $this->getMock(\Magento\Framework\Data\Collection\EntityFactoryInterface::class);
@@ -152,6 +162,30 @@ public function testAddWebsiteFilter()
152162
);
153163
}
154164

165+
public function testAddWebsiteFilterArray()
166+
{
167+
$this->selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
168+
->disableOriginalConstructor()
169+
->getMock();
170+
171+
$this->connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
172+
->disableOriginalConstructor()
173+
->getMockForAbstractClass();
174+
$this->connectionMock->expects($this->atLeastOnce())
175+
->method('quoteInto')
176+
->with($this->equalTo('website. IN (?)'), $this->equalTo(['2', '3']))
177+
->willReturn(true);
178+
179+
$this->abstractCollection->expects($this->atLeastOnce())->method('getSelect')->willReturn($this->selectMock);
180+
$this->abstractCollection->expects($this->atLeastOnce())->method('getConnection')
181+
->willReturn($this->connectionMock);
182+
183+
$this->assertInstanceOf(
184+
\Magento\Rule\Model\ResourceModel\Rule\Collection\AbstractCollection::class,
185+
$this->abstractCollection->addWebsiteFilter(['2', '3'])
186+
);
187+
}
188+
155189
public function testAddFieldToFilter()
156190
{
157191
$this->_prepareAddFilterStubs();

lib/internal/Magento/Framework/DB/Adapter/Pdo/Mysql.php

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Magento\Framework\Phrase;
2727
use Magento\Framework\Stdlib\DateTime;
2828
use Magento\Framework\Stdlib\StringUtils;
29+
use Magento\Framework\DB\Query\Generator as QueryGenerator;
2930

3031
/**
3132
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
@@ -199,6 +200,11 @@ class Mysql extends \Zend_Db_Adapter_Pdo_Mysql implements AdapterInterface
199200
*/
200201
private $exceptionMap;
201202

203+
/**
204+
* @var QueryGenerator
205+
*/
206+
private $queryGenerator;
207+
202208
/**
203209
* @param StringUtils $string
204210
* @param DateTime $dateTime
@@ -3362,57 +3368,32 @@ public function insertFromSelect(Select $select, $table, array $fields = [], $mo
33623368
* @param int $stepCount
33633369
* @return \Magento\Framework\DB\Select[]
33643370
* @throws LocalizedException
3371+
* @deprecated
33653372
*/
33663373
public function selectsByRange($rangeField, \Magento\Framework\DB\Select $select, $stepCount = 100)
33673374
{
3368-
$fromSelect = $select->getPart(\Magento\Framework\DB\Select::FROM);
3369-
if (empty($fromSelect)) {
3370-
throw new LocalizedException(
3371-
new \Magento\Framework\Phrase('Select object must have correct "FROM" part')
3372-
);
3373-
}
3374-
3375-
$tableName = [];
3376-
$correlationName = '';
3377-
foreach ($fromSelect as $correlationName => $formPart) {
3378-
if ($formPart['joinType'] == \Magento\Framework\DB\Select::FROM) {
3379-
$tableName = $formPart['tableName'];
3380-
break;
3381-
}
3382-
}
3383-
3384-
$selectRange = $this->select()
3385-
->from(
3386-
$tableName,
3387-
[
3388-
new \Zend_Db_Expr('MIN(' . $this->quoteIdentifier($rangeField) . ') AS min'),
3389-
new \Zend_Db_Expr('MAX(' . $this->quoteIdentifier($rangeField) . ') AS max'),
3390-
]
3391-
);
3392-
3393-
$rangeResult = $this->fetchRow($selectRange);
3394-
$min = $rangeResult['min'];
3395-
$max = $rangeResult['max'];
3396-
3375+
$iterator = $this->getQueryGenerator()->generate($rangeField, $select, $stepCount);
33973376
$queries = [];
3398-
while ($min <= $max) {
3399-
$partialSelect = clone $select;
3400-
$partialSelect->where(
3401-
$this->quoteIdentifier($correlationName) . '.'
3402-
. $this->quoteIdentifier($rangeField) . ' >= ?',
3403-
$min
3404-
)
3405-
->where(
3406-
$this->quoteIdentifier($correlationName) . '.'
3407-
. $this->quoteIdentifier($rangeField) . ' < ?',
3408-
$min + $stepCount
3409-
);
3410-
$queries[] = $partialSelect;
3411-
$min += $stepCount;
3377+
foreach ($iterator as $query) {
3378+
$queries[] = $query;
34123379
}
34133380
return $queries;
34143381
}
34153382

3383+
/**
3384+
* Get query generator
3385+
*
3386+
* @return QueryGenerator
3387+
* @deprecated
3388+
*/
3389+
private function getQueryGenerator()
3390+
{
3391+
if ($this->queryGenerator === null) {
3392+
$this->queryGenerator = \Magento\Framework\App\ObjectManager::getInstance()->create(QueryGenerator::class);
3393+
}
3394+
return $this->queryGenerator;
3395+
}
3396+
34163397
/**
34173398
* Get update table query using select object for join and update
34183399
*
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\DB\Query;
7+
8+
use Magento\Framework\DB\Adapter\AdapterInterface;
9+
use Magento\Framework\DB\Select;
10+
11+
/**
12+
* Query batch iterator
13+
*/
14+
class BatchIterator implements \Iterator
15+
{
16+
/**
17+
* @var int
18+
*/
19+
private $batchSize;
20+
21+
/**
22+
* @var Select
23+
*/
24+
private $select;
25+
26+
/**
27+
* @var int
28+
*/
29+
private $minValue = 0;
30+
31+
/**
32+
* @var string
33+
*/
34+
private $correlationName;
35+
36+
/**
37+
* @var string
38+
*/
39+
private $rangeField;
40+
41+
/**
42+
* @var Select
43+
*/
44+
private $currentSelect;
45+
46+
/**
47+
* @var AdapterInterface
48+
*/
49+
private $connection;
50+
51+
/**
52+
* @var int
53+
*/
54+
private $iteration = 0;
55+
56+
/**
57+
* @var string
58+
*/
59+
private $rangeFieldAlias;
60+
61+
/**
62+
* @var bool
63+
*/
64+
private $isValid = true;
65+
66+
/**
67+
* Initialize dependencies.
68+
*
69+
* @param Select $select
70+
* @param int $batchSize
71+
* @param string $correlationName
72+
* @param string $rangeField
73+
* @param string $rangeFieldAlias
74+
*/
75+
public function __construct(
76+
Select $select,
77+
$batchSize,
78+
$correlationName,
79+
$rangeField,
80+
$rangeFieldAlias
81+
) {
82+
$this->batchSize = $batchSize;
83+
$this->select = $select;
84+
$this->correlationName = $correlationName;
85+
$this->rangeField = $rangeField;
86+
$this->rangeFieldAlias = $rangeFieldAlias;
87+
$this->connection = $select->getConnection();
88+
}
89+
90+
/**
91+
* @return Select
92+
*/
93+
public function current()
94+
{
95+
if (null == $this->currentSelect) {
96+
$this->currentSelect = $this->initSelectObject();
97+
$itemsCount = $this->calculateBatchSize($this->currentSelect);
98+
$this->isValid = $itemsCount > 0;
99+
}
100+
return $this->currentSelect;
101+
}
102+
103+
/**
104+
* @return Select
105+
*/
106+
public function next()
107+
{
108+
if (null == $this->currentSelect) {
109+
$this->current();
110+
}
111+
$select = $this->initSelectObject();
112+
$itemsCountInSelect = $this->calculateBatchSize($select);
113+
$this->isValid = $itemsCountInSelect > 0;
114+
if ($this->isValid) {
115+
$this->iteration++;
116+
$this->currentSelect = $select;
117+
} else {
118+
$this->currentSelect = null;
119+
}
120+
return $this->currentSelect;
121+
}
122+
123+
/**
124+
* @return int
125+
*/
126+
public function key()
127+
{
128+
return $this->iteration;
129+
}
130+
131+
/**
132+
* @return bool
133+
*/
134+
public function valid()
135+
{
136+
return $this->isValid;
137+
}
138+
139+
/**
140+
* @return void
141+
*/
142+
public function rewind()
143+
{
144+
$this->minValue = 0;
145+
$this->currentSelect = null;
146+
$this->iteration = 0;
147+
$this->isValid = true;
148+
}
149+
150+
/**
151+
* Calculate batch size for select.
152+
*
153+
* @param Select $select
154+
* @return int
155+
*/
156+
private function calculateBatchSize(Select $select)
157+
{
158+
$wrapperSelect = $this->connection->select();
159+
$wrapperSelect->from(
160+
$select,
161+
[
162+
new \Zend_Db_Expr('MAX(' . $this->rangeFieldAlias . ') as max'),
163+
new \Zend_Db_Expr('COUNT(*) as cnt')
164+
]
165+
);
166+
$row = $this->connection->fetchRow($wrapperSelect);
167+
$this->minValue = $row['max'];
168+
return intval($row['cnt']);
169+
}
170+
171+
/**
172+
* Initialize select object.
173+
*
174+
* @return \Magento\Framework\DB\Select
175+
*/
176+
private function initSelectObject()
177+
{
178+
$object = clone $this->select;
179+
$object->where(
180+
$this->connection->quoteIdentifier($this->correlationName)
181+
. '.' . $this->connection->quoteIdentifier($this->rangeField)
182+
. ' > ?',
183+
$this->minValue
184+
);
185+
$object->limit($this->batchSize);
186+
/**
187+
* Reset sort order section from origin select object
188+
*/
189+
$object->order($this->correlationName . '.' . $this->rangeField . ' ' . \Magento\Framework\DB\Select::SQL_ASC);
190+
return $object;
191+
}
192+
}

0 commit comments

Comments
 (0)