public function testMultiAdvanced()
{
$this->insertBookData();
$collection = R::findMulti('book,page,text,category', '
SELECT book.*, page.*, text.*, category.*
FROM book
LEFT JOIN page ON page.book_id = book.id
LEFT JOIN text ON text.page_id = page.id
LEFT JOIN book_category ON book_category.book_id = book.id
LEFT JOIN category ON book_category.category_id = category.id
');
asrt(count($collection), 4);
asrt(isset($collection['book']), TRUE);
asrt(isset($collection['page']), TRUE);
asrt(isset($collection['text']), TRUE);
asrt(isset($collection['category']), TRUE);
asrt(count($collection['book']), 5);
asrt(count($collection['page']), 9);
asrt(count($collection['text']), 11);
asrt(count($collection['category']), 3);
foreach ($collection['book'] as $bean) {
asrt($bean instanceof OODBBean, TRUE);
}
foreach ($collection['page'] as $bean) {
asrt($bean instanceof OODBBean, TRUE);
}
foreach ($collection['text'] as $bean) {
asrt($bean instanceof OODBBean, TRUE);
}
foreach ($collection['category'] as $bean) {
asrt($bean instanceof OODBBean, TRUE);
}
foreach ($collection['book'] as $book) {
$titles[] = $book->title;
}
asrt(in_array('Diehard C', $titles), TRUE);
asrt(in_array('Adventures in JavaScript', $titles), TRUE);
asrt(in_array('CSS ala Picasso', $titles), TRUE);
asrt(in_array('PHP Tips and Tricks', $titles), TRUE);
asrt(in_array('Secrets of SQL', $titles), TRUE);
$collection = R::findMulti('book,page,text,category,book_category', '
SELECT book.*, page.*, text.*, category.*, book_category.*
FROM book
LEFT JOIN page ON page.book_id = book.id
LEFT JOIN text ON text.page_id = page.id
LEFT JOIN book_category ON book_category.book_id = book.id
LEFT JOIN category ON book_category.category_id = category.id
WHERE category_id > ?
ORDER BY book.title ASC
', array(0), array(array('b' => 'page', 'a' => 'text', 'do' => function ($a, $b) {
$b->noLoad()->ownTextList[] = $a;
$b->clearHistory();
}, 'matcher' => function ($a, $b) {
return $a->page_id == $b->id;
}), array('b' => 'book', 'a' => 'page', 'do' => function ($a, $b) {
$b->noLoad()->ownPageList[] = $a;
$b->clearHistory();
}, 'matcher' => function ($a, $b) {
return $a->book_id == $b->id;
}), array('b' => 'category', 'a' => 'book', 'do' => function ($a, $b) {
$a->noLoad()->sharedCategoryList[] = $b;
$a->clearHistory();
}, 'matcher' => function ($a, $b, $beans) {
foreach ($beans['book_category'] as $bean) {
if ($bean->book_id == $a->id && $bean->category_id == $b->id) {
return TRUE;
}
}
return FALSE;
})));
$books = $collection['book'];
$book = reset($books);
asrt($book->title, 'Adventures in JavaScript');
R::nuke();
asrt(count($book->ownPageList), 3);
$page = reset($book->ownPageList);
asrt(count($page->ownTextList), 1);
asrt(count($book->sharedCategoryList), 2);
$categories = array();
foreach ($book->sharedCategoryList as $category) {
$categories[] = $category->name;
}
sort($categories);
asrt(implode(',', $categories), 'Programming,Web Development');
$book = next($books);
asrt($book->title, 'CSS ala Picasso');
asrt(count($book->ownPage), 1);
$page = reset($book->ownPage);
asrt(count($page->ownTextList), 2);
$texts = array();
foreach ($page->ownTextList as $text) {
$texts[] = $text->content;
}
asrt(in_array('Now we use it for applications...', $texts), TRUE);
$categories = array();
foreach ($book->sharedCategoryList as $category) {
$categories[] = $category->name;
}
sort($categories);
asrt(implode(',', $categories), 'Design,Programming,Web Development');
$book = next($books);
asrt($book->title, 'Diehard C');
asrt(count($book->ownPageList), 2);
$page = reset($book->ownPageList);
asrt(count($page->ownTextList), 2);
$page = next($book->ownPageList);
asrt(count($page->ownTextList), 1);
$categories = array();
foreach ($book->sharedCategoryList as $category) {
$categories[] = $category->name;
}
sort($categories);
asrt(implode(',', $categories), 'Programming');
//should have no effect, nothing should have changed
R::storeAll($books);
asrt(R::count('book'), 0);
asrt(R::count('page'), 0);
asrt(R::count('text'), 0);
}