55use Oro \Bundle \DataGridBundle \Datagrid \ParameterBag ;
66use Oro \Bundle \DataGridBundle \Datagrid \Common \DatagridConfiguration ;
77use Oro \Bundle \DataGridBundle \Event \PreBuild ;
8- use Oro \Bundle \DataGridBundle \Extension \Sorter \OrmSorterExtension ;
98use Oro \Bundle \FilterBundle \Grid \Extension \OrmFilterExtension ;
109
1110class 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}
0 commit comments