55use Zend \Db \Sql \Select ;
66use Zend \EntityMapper \Config \Container \Container ;
77use Zend \EntityMapper \Config \Entity ;
8+ use Zend \EntityMapper \Db \Select \Reflection \JoinReflector ;
89use Zend \EntityMapper \Db \Select \Reflection \OperatorReflector ;
910use Zend \EntityMapper \Db \Select \Reflection \SelectReflector ;
1011
@@ -169,6 +170,56 @@ public function parseOrder(): Select
169170 $ order ->setValue ($ select , $ parsedOrders );
170171
171172 return $ select ;
173+ }
174+
175+ /**
176+ * @return Select
177+ * @throws \Zend\EntityMapper\Config\Container\Exceptions\ItemNotFoundException
178+ */
179+ public function parseJoin (): Select
180+ {
181+ $ joins = $ this ->reflector ->getProperty ('joins ' );
182+ $ joinReflector = new JoinReflector ($ joins );
183+ $ joinClauses = $ joinReflector ->getJoinClauses ();
184+ $ parsedJoinClauses = [];
185+
186+ foreach ($ joinClauses as $ joinClause ) {
187+
188+ $ tableName = null ;
189+ $ rawObjectName = $ joinClause ['name ' ];
190+
191+ if (is_array ($ rawObjectName )) {
192+ foreach ($ rawObjectName as $ alias => $ class ) {
193+ $ config = $ this ->container ->get ($ class );
194+ $ joinClause ['name ' ] = [$ alias => $ config ->getTable ()];
195+ $ fields = $ config ->getFields ();
196+ }
197+ }
198+ else {
199+ $ config = $ this ->container ->get ($ rawObjectName );
200+ $ fields = $ config ->getFields ();
201+ }
172202
203+ foreach ($ fields as $ field ) {
204+ $ joinClause ['on ' ] = str_replace ($ field ->getProperty (), $ field ->getAlias (), $ joinClause ['on ' ]);
205+
206+ foreach ($ joinClause ['columns ' ] as $ key => $ column ) {
207+ $ joinClause ['columns ' ][$ key ] = str_replace ($ field ->getProperty (), $ field ->getAlias (), $ joinClause ['columns ' ][$ key ]);
208+ }
209+ }
210+
211+ $ parsedJoinClauses [] = $ joinClause ;
212+
213+ }
214+
215+ $ joinsReflection = new \ReflectionObject ($ joins );
216+ $ joinsReflectorJoinsProperty = $ joinsReflection ->getProperty ('joins ' );
217+ $ joinsReflectorJoinsProperty ->setAccessible (true );
218+ $ joinsReflectorJoinsProperty ->setValue ($ joins , $ parsedJoinClauses );
219+
220+ $ this ->reflector ->setProperty ('joins ' , $ joins );
221+ $ select = $ this ->reflector ->getSelect ();
222+
223+ return $ select ;
173224 }
174225}
0 commit comments