public function testJoinSeveralManyManyInARow() { $b = new SelectBuilder(); $b->root('Article'); $b->where('readers/followers/id', 12); $b->select(['*'], false); $components = $b->build(); $jc[] = new JoinClause('articles', '_'); $jc[] = (new JoinClause('articles_USERS', 'readers_m'))->on('_', 'id', 'readers_m', 'article_id'); $jc[] = (new JoinClause('USERS', 'readers'))->on('readers_m', 'user_id', 'readers', 'id'); $jc[] = (new JoinClause('USERS_USERS', 'readers.followers_m'))->on('readers', 'id', 'readers.followers_m', 'user_id'); $jc[] = (new JoinClause('USERS', 'readers.followers'))->on('readers.followers_m', 'user_id', 'readers.followers', 'id'); $this->assertEquals($jc, $components['from']); }
public function testWhereWithQueryAsValue() { $b = new SelectBuilder(); $b->root('Blog'); $subQuery = User::query(null, 'articles.readers')->whereRelation(Article::relation('readers'), 'articles')->addAggregate('AVG', 'age')->getQuery(); $b->where('articles/author/age', '>', $subQuery); $b->get(['*'], false); $components = $b->build(); $values = $b->getValues(); $c = new BaseCompiler(); $sql = $c->compileComponents($components, 'select'); $val = $c->compileValues($values, 'select'); $expected = 'SELECT `_`.* FROM `blogs` `_` INNER JOIN `articles` `articles` ON `_`.`id` = `articles`.`blog_id` INNER JOIN `authors` `articles.author` ON `articles`.`author_id` = `articles.author`.`id` WHERE `articles.author`.`age` > (SELECT AVG(`articles.readers`.`age`) FROM `USERS` `articles.readers` INNER JOIN `articles_USERS` `articles.readers_m` ON `articles.readers`.`id` = `articles.readers_m`.`user_id` WHERE `articles.readers_m`.`article_id` = `articles`.`id`)'; $expectedValues = [[]]; $this->assertEquals($expected, $sql); $this->assertEquals($expectedValues, $val); }