/** * @inheritdoc */ public function toCriteria() { $criteria = new Criteria(); if ($this->filterTransfer->getLimit() !== null) { $criteria->setLimit($this->filterTransfer->getLimit()); } if ($this->filterTransfer->getOffset() !== null) { $criteria->setOffset($this->filterTransfer->getOffset()); } if ($this->filterTransfer->getOrderBy() !== null) { if ($this->filterTransfer->getOrderDirection() === 'ASC') { $criteria->addAscendingOrderByColumn($this->filterTransfer->getOrderBy()); } elseif ($this->filterTransfer->getOrderDirection() === 'DESC') { $criteria->addDescendingOrderByColumn($this->filterTransfer->getOrderBy()); } } return $criteria; }
public function testMssqlApplyLimitWithOffsetMultipleOrderBy() { $db = Propel::getServiceContainer()->getAdapter(BookTableMap::DATABASE_NAME); if (!$db instanceof MssqlAdapter) { $this->markTestSkipped('Configured database vendor is not MsSQL'); } $c = new Criteria(BookTableMap::DATABASE_NAME); $c->addSelectColumn(BookTableMap::COL_ID); $c->addSelectColumn(BookTableMap::COL_TITLE); $c->addSelectColumn(PublisherTableMap::COL_NAME); $c->addAsColumn('PublisherName', '(SELECT MAX(publisher.NAME) FROM publisher WHERE publisher.ID = book.PUBLISHER_ID)'); $c->addJoin(BookTableMap::COL_PUBLISHER_ID, PublisherTableMap::COL_ID, Criteria::LEFT_JOIN); $c->addDescendingOrderByColumn('PublisherName'); $c->addAscendingOrderByColumn(BookTableMap::COL_TITLE); $c->setOffset(20); $c->setLimit(20); $params = array(); $expectedSql = "SELECT [book.ID], [book.TITLE], [publisher.NAME], [PublisherName] FROM (SELECT ROW_NUMBER() OVER(ORDER BY (SELECT MAX(publisher.NAME) FROM publisher WHERE publisher.ID = book.PUBLISHER_ID) DESC, book.TITLE ASC) AS [RowNumber], book.ID AS [book.ID], book.TITLE AS [book.TITLE], publisher.NAME AS [publisher.NAME], (SELECT MAX(publisher.NAME) FROM publisher WHERE publisher.ID = book.PUBLISHER_ID) AS [PublisherName] FROM book LEFT JOIN publisher ON (book.PUBLISHER_ID=publisher.ID)) AS derivedb WHERE RowNumber BETWEEN 21 AND 40"; $sql = $c->createSelectSql($params); $this->assertEquals($expectedSql, $sql); }
public function testLimit() { $c = new Criteria(); $this->assertEquals(0, $c->getLimit(), 'Limit is 0 by default'); $c2 = $c->setLimit(1); $this->assertEquals(1, $c->getLimit(), 'Limit is set by setLimit'); $this->assertSame($c, $c2, 'setLimit() returns the current Criteria'); }
public function testMergeWithFurtherModified() { $c1 = new Criteria(); $c2 = new Criteria(); $c2->setLimit(123); $c1->mergeWith($c2); $this->assertEquals(123, $c1->getLimit(), 'mergeWith() makes the merge'); $c2->setLimit(456); $this->assertEquals(123, $c1->getLimit(), 'further modifying a merged criteria does not affect the merger'); }
public function testPopulateRelationCriteria() { AuthorTableMap::clearInstancePool(); BookTableMap::clearInstancePool(); $authors = AuthorQuery::create()->find(); $c = new Criteria(); $c->setLimit(3); $books = $authors->populateRelation('Book', $c); $this->assertEquals(3, count($books), 'populateRelation() accepts an optional criteria object to filter the query'); }
/** * Tests performing doSelect() and doSelectJoin() using LIMITs. */ public function testDoSelect_Limit() { // 1) get the total number of items in a particular table $count = BookQuery::create()->count(); $this->assertTrue($count > 1, "Need more than 1 record in books table to perform this test."); $limitcount = $count - 1; $lc = new Criteria(); $lc->setLimit($limitcount); $results = BookQuery::create(null, $lc)->find(); $this->assertEquals($limitcount, count($results), "Expected {$limitcount} results from BookQuery::doSelect()"); // re-create it just to avoid side-effects $lc2 = new Criteria(); $lc2->setLimit($limitcount); $results2 = BookQuery::create(null, $lc2)->joinWith('Author')->find(); $this->assertEquals($limitcount, count($results2), "Expected {$limitcount} results from BookQuery::doSelectJoinAuthor()"); }