@@ -146,6 +146,71 @@ public function testMagicJoin() {
146146 $ this ->assertEquals ($ expectedSql , self ::simplifySql ($ magicQuery ->build ($ sql )));
147147 }
148148
149+ public function testMagicJoin2 () {
150+ $ schema = new Schema ();
151+ $ role = $ schema ->createTable ("role " );
152+ $ role ->addColumn ("id " , "integer " , array ("unsigned " => true ));
153+ $ role ->addColumn ("label " , "string " , array ("length " => 32 ));
154+
155+ $ right = $ schema ->createTable ("right " );
156+ $ right ->addColumn ("id " , "integer " , array ("unsigned " => true ));
157+ $ right ->addColumn ("label " , "string " , array ("length " => 32 ));
158+ $ role_right = $ schema ->createTable ("role_right " );
159+
160+ $ role_right ->addColumn ("role_id " , "integer " , array ("unsigned " => true ));
161+ $ role_right ->addColumn ("right_id " , "integer " , array ("unsigned " => true ));
162+ $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('role ' ), array ("role_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
163+ $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
164+ $ role_right ->setPrimaryKey (["role_id " , "right_id " ]);
165+
166+ $ user = $ schema ->createTable ("user " );
167+ $ user ->addColumn ("id " , "integer " , array ("unsigned " => true ));
168+ $ user ->addColumn ("login " , "string " , array ("length " => 32 ));
169+ $ user ->addColumn ("role_id " , "integer " , array ("unsigned " => true ));
170+ $ user ->addForeignKeyConstraint ($ schema ->getTable ('role ' ), array ("role_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
171+
172+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ($ schema ));
173+
174+ $ magicQuery = new MagicQuery (null , null , $ schemaAnalyzer );
175+
176+ $ sql = "SELECT role.* FROM magicjoin(role) WHERE right.label = 'my_right' AND user.login = 'foo' " ;
177+ $ expectedSql = "SELECT role.* FROM role LEFT JOIN role_right ON (role_right.role_id = role.id) LEFT JOIN right ON (role_right.right_id = right.id) LEFT JOIN user ON (user.role_id = role.id) WHERE (right.label = 'my_right') AND (user.login = 'foo') " ;
178+ $ this ->assertEquals ($ expectedSql , self ::simplifySql ($ magicQuery ->build ($ sql )));
179+ }
180+
181+ public function testMagicJoin3 () {
182+ $ schema = new Schema ();
183+ $ role = $ schema ->createTable ("role " );
184+ $ role ->addColumn ("id " , "integer " , array ("unsigned " => true ));
185+ $ role ->addColumn ("label " , "string " , array ("length " => 32 ));
186+
187+ $ right = $ schema ->createTable ("right " );
188+ $ right ->addColumn ("id " , "integer " , array ("unsigned " => true ));
189+ $ right ->addColumn ("label " , "string " , array ("length " => 32 ));
190+ $ role_right = $ schema ->createTable ("role_right " );
191+
192+ $ role_right ->addColumn ("role_id " , "integer " , array ("unsigned " => true ));
193+ $ role_right ->addColumn ("right_id " , "integer " , array ("unsigned " => true ));
194+ $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('role ' ), array ("role_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
195+ $ role_right ->addForeignKeyConstraint ($ schema ->getTable ('right ' ), array ("right_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
196+ $ role_right ->setPrimaryKey (["role_id " , "right_id " ]);
197+
198+ $ status = $ schema ->createTable ("status " );
199+ $ status ->addColumn ("id " , "integer " , array ("unsigned " => true ));
200+ $ status ->addColumn ("name " , "string " , array ("length " => 32 ));
201+
202+ $ role ->addColumn ("status_id " , "integer " , array ("unsigned " => true ));
203+ $ role ->addForeignKeyConstraint ($ schema ->getTable ('status ' ), array ("status_id " ), array ("id " ), array ("onUpdate " => "CASCADE " ));
204+
205+ $ schemaAnalyzer = new SchemaAnalyzer (new StubSchemaManager ($ schema ));
206+
207+ $ magicQuery = new MagicQuery (null , null , $ schemaAnalyzer );
208+
209+ $ sql = "SELECT role.* FROM magicjoin(role) WHERE right.label = 'my_right' AND status.name = 'foo' " ;
210+ $ expectedSql = "SELECT role.* FROM role LEFT JOIN role_right ON (role_right.role_id = role.id) LEFT JOIN right ON (role_right.right_id = right.id) LEFT JOIN status ON (role.status_id = status.id) WHERE (right.label = 'my_right') AND (status.name = 'foo') " ;
211+ $ this ->assertEquals ($ expectedSql , self ::simplifySql ($ magicQuery ->build ($ sql )));
212+ }
213+
149214 /**
150215 * @expectedException \Mouf\Database\MagicQueryMissingConnectionException
151216 */
0 commit comments