コード例 #1
0
 public function testSubQueryRecursive()
 {
     // sort the books (on date, if equal continue with id), filtered by a publisher
     $sortedBookQuery = new BookQuery();
     $sortedBookQuery->addSelfSelectColumns();
     $sortedBookQuery->filterByPublisherId(123);
     $sortedBookQuery->orderByTitle(Criteria::DESC);
     $sortedBookQuery->orderById(Criteria::DESC);
     // group by author, after sorting!
     $latestBookQuery = new BookQuery();
     $latestBookQuery->addSelectQuery($sortedBookQuery, 'sortedBookQuery');
     $latestBookQuery->groupBy('sortedBookQuery.AuthorId');
     // filter from these latest books, find the ones cheaper than 12 euro
     $c = new BookQuery();
     $c->addSelectQuery($latestBookQuery, 'latestBookQuery');
     $c->filterByPrice(12, Criteria::LESS_THAN);
     $sql = "SELECT latestBookQuery.ID, latestBookQuery.TITLE, latestBookQuery.ISBN, latestBookQuery.PRICE, latestBookQuery.PUBLISHER_ID, latestBookQuery.AUTHOR_ID " . "FROM (SELECT sortedBookQuery.ID, sortedBookQuery.TITLE, sortedBookQuery.ISBN, sortedBookQuery.PRICE, sortedBookQuery.PUBLISHER_ID, sortedBookQuery.AUTHOR_ID " . "FROM (SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID " . "FROM `book` " . "WHERE book.PUBLISHER_ID=:p2 " . "ORDER BY book.TITLE DESC,book.ID DESC) AS sortedBookQuery " . "GROUP BY sortedBookQuery.AUTHOR_ID) AS latestBookQuery " . "WHERE latestBookQuery.PRICE<:p1";
     $params = array(array('table' => 'book', 'column' => 'PRICE', 'value' => 12), array('table' => 'book', 'column' => 'PUBLISHER_ID', 'value' => 123));
     $this->assertCriteriaTranslation($c, $sql, $params, 'addSubQueryCriteriaInFrom() combines two queries succesfully');
 }