Skip to content

Commit 3f8f955

Browse files
committed
Merge remote-tracking branch 'remotes/dev/1.10' into 1.10
2 parents 129121b + ba17325 commit 3f8f955

File tree

11 files changed

+531
-301
lines changed

11 files changed

+531
-301
lines changed

src/OroCRM/Bundle/MagentoBundle/EventListener/CustomerDataGridListener.php

Lines changed: 0 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Oro\Bundle\DataGridBundle\Datagrid\ParameterBag;
66
use Oro\Bundle\DataGridBundle\Datagrid\Common\DatagridConfiguration;
77
use Oro\Bundle\DataGridBundle\Event\PreBuild;
8-
use Oro\Bundle\DataGridBundle\Extension\Sorter\OrmSorterExtension;
98
use Oro\Bundle\FilterBundle\Grid\Extension\OrmFilterExtension;
109

1110
class CustomerDataGridListener
@@ -18,7 +17,6 @@ public function onPreBuild(PreBuild $event)
1817
$config = $event->getConfig();
1918
$parameters = $event->getParameters();
2019
$this->addNewsletterSubscribers($config, $parameters);
21-
$this->convertJoinsToSubQueries($config, $parameters);
2220
}
2321

2422
/**
@@ -79,158 +77,4 @@ protected function addNewsletterSubscribers(DatagridConfiguration $config, Param
7977
];
8078
$config->offsetSetByPath('[source][query]', $query);
8179
}
82-
83-
/**
84-
* @param DatagridConfiguration $config
85-
* @param ParameterBag $parameters
86-
*/
87-
protected function convertJoinsToSubQueries(DatagridConfiguration $config, ParameterBag $parameters)
88-
{
89-
// by a performance reasons, convert some joins to sub-queries
90-
$sorters = $parameters->get(OrmSorterExtension::SORTERS_ROOT_PARAM, []);
91-
if (empty($sorters['channelName'])) {
92-
$this->convertAssociationJoinToSubquery(
93-
$config,
94-
'dataChannel',
95-
'channelName',
96-
'OroCRM\Bundle\ChannelBundle\Entity\Channel'
97-
);
98-
}
99-
if (empty($sorters['websiteName'])) {
100-
$this->convertAssociationJoinToSubquery(
101-
$config,
102-
'cw',
103-
'websiteName',
104-
'OroCRM\Bundle\MagentoBundle\Entity\Website'
105-
);
106-
}
107-
if (empty($sorters['customerGroup'])) {
108-
$this->convertAssociationJoinToSubquery(
109-
$config,
110-
'cg',
111-
'customerGroup',
112-
'OroCRM\Bundle\MagentoBundle\Entity\CustomerGroup'
113-
);
114-
}
115-
}
116-
117-
/**
118-
* @param DatagridConfiguration $config
119-
* @param string $joinAlias
120-
* @param string $columnAlias
121-
* @param string $joinEntityClass
122-
*/
123-
private function convertAssociationJoinToSubquery(
124-
DatagridConfiguration $config,
125-
$joinAlias,
126-
$columnAlias,
127-
$joinEntityClass
128-
) {
129-
list(
130-
$join,
131-
$joinPath,
132-
$selectExpr,
133-
$selectPath
134-
) = $this->findJoinAndSelectByAliases($config, $joinAlias, $columnAlias);
135-
if (!$join || !$selectExpr) {
136-
return;
137-
}
138-
139-
$subQuery = sprintf(
140-
'SELECT %1$s FROM %4$s AS %3$s WHERE %3$s = %2$s',
141-
$selectExpr,
142-
$join['join'],
143-
$joinAlias,
144-
$joinEntityClass
145-
);
146-
if (!empty($join['condition'])) {
147-
$subQuery .= sprintf(' AND %s', $join['condition']);
148-
}
149-
150-
$config->offsetSetByPath($selectPath, sprintf('(%s) AS %s', $subQuery, $columnAlias));
151-
$config->offsetUnsetByPath($joinPath);
152-
}
153-
154-
/**
155-
* @param DatagridConfiguration $config
156-
* @param string $joinAlias
157-
* @param string $columnAlias
158-
*
159-
* @return array [join, join path, select expression without column alias, select item path]
160-
*/
161-
private function findJoinAndSelectByAliases(DatagridConfiguration $config, $joinAlias, $columnAlias)
162-
{
163-
list($join, $joinPath) = $this->findJoinByAlias($config, $joinAlias, '[source][query][join][left]');
164-
$selectExpr = null;
165-
$selectPath = null;
166-
if (null !== $join) {
167-
list($selectExpr, $selectPath) = $this->findSelectExprByAlias($config, $columnAlias);
168-
}
169-
170-
return [$join, $joinPath, $selectExpr, $selectPath];
171-
}
172-
173-
/**
174-
* @param DatagridConfiguration $config
175-
* @param string $joinAlias
176-
* @param string $joinsPath
177-
*
178-
* @return array [join, join path]
179-
*/
180-
private function findJoinByAlias(DatagridConfiguration $config, $joinAlias, $joinsPath)
181-
{
182-
$foundJoin = null;
183-
$foundJoinPath = null;
184-
$joins = $config->offsetGetByPath($joinsPath, []);
185-
foreach ($joins as $key => $join) {
186-
if ($join['alias'] === $joinAlias) {
187-
$foundJoin = $join;
188-
$foundJoinPath = sprintf('%s[%s]', $joinsPath, $key);
189-
break;
190-
}
191-
}
192-
193-
return [$foundJoin, $foundJoinPath];
194-
}
195-
196-
/**
197-
* @param DatagridConfiguration $config
198-
* @param string $columnAlias
199-
*
200-
* @return array [select expression without column alias, select item path]
201-
*/
202-
private function findSelectExprByAlias(DatagridConfiguration $config, $columnAlias)
203-
{
204-
$foundSelectExpr = null;
205-
$foundSelectPath = null;
206-
$pattern = sprintf('#(?P<expr>.+?)\\s+AS\\s+%s#i', $columnAlias);
207-
$selects = $config->offsetGetByPath('[source][query][select]', []);
208-
foreach ($selects as $key => $select) {
209-
if (preg_match($pattern, $select, $matches)) {
210-
$foundSelectExpr = $matches['expr'];
211-
$foundSelectPath = sprintf('[source][query][select][%s]', $key);
212-
break;
213-
}
214-
}
215-
216-
return [$foundSelectExpr, $foundSelectPath];
217-
}
218-
219-
/**
220-
* @param DatagridConfiguration $config
221-
*
222-
* @return string|null
223-
*/
224-
private function getRootAlias(DatagridConfiguration $config)
225-
{
226-
$fromPart = $config->offsetGetByPath('[source][query][from]', []);
227-
if (empty($fromPart)) {
228-
return null;
229-
}
230-
$from = reset($fromPart);
231-
232-
return array_key_exists('alias', $from)
233-
? $from['alias']
234-
: null;
235-
}
23680
}

src/OroCRM/Bundle/MagentoBundle/Tests/Unit/EventListener/CustomerDataGridListenerTest.php

Lines changed: 0 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -162,107 +162,4 @@ public function testAddNewsletterSubscribersWhenFilteringByIsSubscriberWasReques
162162
$config->toArray()
163163
);
164164
}
165-
166-
public function testConvertJoinsToSubQueriesWhenSortingWasNotRequested()
167-
{
168-
$parameters = new ParameterBag();
169-
170-
$config = DatagridConfiguration::create(
171-
[
172-
'source' => [
173-
'query' => [
174-
'select' => [
175-
'c.id',
176-
'dataChannel.name as channelName',
177-
'cw.name as websiteName',
178-
'cg.name as customerGroup'
179-
],
180-
'from' => [
181-
['table' => 'OroCRM\Bundle\MagentoBundle\Entity\Customer', 'alias' => 'c']
182-
],
183-
'join' => [
184-
'left' => [
185-
['join' => 'c.dataChannel', 'alias' => 'dataChannel'],
186-
['join' => 'c.website', 'alias' => 'cw'],
187-
['join' => 'c.group', 'alias' => 'cg']
188-
]
189-
]
190-
]
191-
]
192-
]
193-
);
194-
195-
$this->listener->onPreBuild(new PreBuild($config, $parameters));
196-
197-
$this->assertEquals(
198-
[
199-
'source' => [
200-
'query' => [
201-
'select' => [
202-
'c.id',
203-
'(SELECT dataChannel.name FROM OroCRM\Bundle\ChannelBundle\Entity\Channel AS dataChannel'
204-
. ' WHERE dataChannel = c.dataChannel) AS channelName',
205-
'(SELECT cw.name FROM OroCRM\Bundle\MagentoBundle\Entity\Website AS cw'
206-
. ' WHERE cw = c.website) AS websiteName',
207-
'(SELECT cg.name FROM OroCRM\Bundle\MagentoBundle\Entity\CustomerGroup AS cg'
208-
. ' WHERE cg = c.group) AS customerGroup'
209-
],
210-
'from' => [
211-
['table' => 'OroCRM\Bundle\MagentoBundle\Entity\Customer', 'alias' => 'c']
212-
],
213-
'join' => [
214-
'left' => []
215-
]
216-
]
217-
]
218-
],
219-
$config->toArray(['source'])
220-
);
221-
}
222-
223-
public function testConvertJoinsToSubQueriesWhenSortingWasRequested()
224-
{
225-
$parameters = new ParameterBag();
226-
$parameters->set(
227-
'_sort_by',
228-
[
229-
'channelName' => '1',
230-
'websiteName' => '2',
231-
'customerGroup' => '3'
232-
]
233-
);
234-
235-
$config = DatagridConfiguration::create(
236-
[
237-
'source' => [
238-
'query' => [
239-
'select' => [
240-
'c.id',
241-
'dataChannel.name as channelName',
242-
'cw.name as websiteName',
243-
'cg.name as customerGroup'
244-
],
245-
'from' => [
246-
['table' => 'OroCRM\Bundle\MagentoBundle\Entity\Customer', 'alias' => 'c']
247-
],
248-
'join' => [
249-
'left' => [
250-
['join' => 'c.dataChannel', 'alias' => 'dataChannel'],
251-
['join' => 'c.website', 'alias' => 'cw'],
252-
['join' => 'c.group', 'alias' => 'cg']
253-
]
254-
]
255-
]
256-
]
257-
]
258-
);
259-
$originalConfig = $config->toArray(['source']);
260-
261-
$this->listener->onPreBuild(new PreBuild($config, $parameters));
262-
263-
$this->assertEquals(
264-
$originalConfig,
265-
$config->toArray(['source'])
266-
);
267-
}
268165
}

0 commit comments

Comments
 (0)