private function testComplexCombinationsJoins()
{
$author = R::dispense('author');
$books = R::dispense('book', 4);
$books[0]->num = 0;
$books[1]->num = 1;
$books[2]->num = 2;
$books[3]->num = 3;
$books[0]->info = R::dispense('info')->setAttr('title', 'Learning PHP');
$books[1]->info = R::dispense('info')->setAttr('title', 'Learning PHP and JavaScript');
$books[2]->info = R::dispense('info')->setAttr('title', 'Learning Cobol');
$books[3]->info = R::dispense('info')->setAttr('title', 'Gardening for Beginners');
$books[0]->category = R::dispense('category')->setAttr('title', 'computers');
$books[1]->category = R::dispense('category')->setAttr('title', 'computers');
$books[2]->category = R::dispense('category')->setAttr('title', 'computers');
$books[3]->category = R::dispense('category')->setAttr('title', 'gardening');
$author->ownBookList = $books;
R::store($author);
//Base test...
$books = $author->ownBookList;
$this->checkBookNumbers($books, '0,1,2,3');
//Just a basic Join...
$books = $author->withCondition(' @joined.info.title LIKE ? ORDER BY book.num ASC ', array('%PHP%'))->ownBookList;
$this->checkBookNumbers($books, '0,1');
//Mix Join and criteria
$books = $author->withCondition(' @joined.info.title LIKE ? AND num > 0 ORDER BY book.num ASC ', array('%PHP%'))->ownBookList;
$this->checkBookNumbers($books, '1');
//Basic join
$books = $author->withCondition(' @joined.info.title LIKE ? ORDER BY book.num ASC', array('%ing%'))->ownBookList;
$this->checkBookNumbers($books, '0,1,2,3');
//Two joins
$books = $author->withCondition(' @joined.info.title LIKE ? AND @joined.category.title = ? ORDER BY book.num ASC', array('%ing%', 'computers'))->ownBookList;
$this->checkBookNumbers($books, '0,1,2');
//Join the same type twice... and order
$books = $author->withCondition(' @joined.info.title LIKE ? AND @joined.category.title = ? ORDER BY @joined.info.title ASC ', array('%ing%', 'computers'))->ownBookList;
$this->checkBookNumbers($books, '2,0,1');
//Join the same type twice
$books = $author->withCondition(' @joined.info.title LIKE ? AND @joined.info.title LIKE ? ORDER BY book.num ASC', array('%ing%', '%Learn%'))->ownBookList;
$this->checkBookNumbers($books, '0,1,2');
//Join the same type 3 times and order
$books = $author->withCondition(' @joined.info.title LIKE ? AND @joined.info.title LIKE ? ORDER BY @joined.info.title DESC', array('%ing%', '%Learn%'))->ownBookList;
$this->checkBookNumbers($books, '1,0,2');
//Join the same type 3 times and order and limit
$books = $author->withCondition(' @joined.info.title LIKE ? AND @joined.info.title LIKE ? ORDER BY @joined.info.title DESC LIMIT 1', array('%ing%', '%Learn%'))->ownBookList;
$this->checkBookNumbers($books, '1');
//Other combinations I can think of...
$books = $author->withCondition(' @joined.category.title LIKE ? ORDER BY @joined.info.title DESC', array('%ing%'))->ownBookList;
$this->checkBookNumbers($books, '3');
$books = $author->withCondition(' @joined.category.title LIKE ? AND num < 4 ORDER BY @joined.info.title DESC', array('%ing%'))->ownBookList;
$this->checkBookNumbers($books, '3');
//multiple ordering
$books = $author->with(' ORDER BY @joined.category.title ASC, @joined.info.title ASC')->ownBookList;
$this->checkBookNumbers($books, '2,0,1,3');
$books = $author->with(' ORDER BY @joined.category.title DESC, @joined.info.title ASC')->ownBookList;
$this->checkBookNumbers($books, '3,2,0,1');
$books = $author->with(' ORDER BY @joined.category.title DESC, @joined.info.title ASC LIMIT 2')->ownBookList;
$this->checkBookNumbers($books, '3,2');
}