本文整理匯總了PHP中Propel\Tests\Bookstore\BookQuery類的典型用法代碼示例。如果您正苦於以下問題:PHP BookQuery類的具體用法?PHP BookQuery怎麽用?PHP BookQuery使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了BookQuery類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: testFrom
public function testFrom()
{
$q = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book');
$expected = new BookQuery();
$this->assertEquals($expected, $q, 'from() returns a Model query instance based on the model name');
$q = PropelQuery::from('\\Propel\\Tests\\Bookstore\\Book b');
$expected = new BookQuery();
$expected->setModelAlias('b');
$this->assertEquals($expected, $q, 'from() sets the model alias if found after the blank');
$q = PropelQuery::from('\\Propel\\Tests\\Runtime\\Query\\myBook');
$expected = new myBookQuery();
$this->assertEquals($expected, $q, 'from() can find custom query classes');
try {
$q = PropelQuery::from('Foo');
$this->fail('PropelQuery::from() throws an exception when called on a non-existing query class');
} catch (PropelException $e) {
$this->assertTrue(true, 'PropelQuery::from() throws an exception when called on a non-existing query class');
}
}
示例2: testSerializeHydratedObject
public function testSerializeHydratedObject()
{
$book = new Book();
$book->setTitle('Foo3');
$book->setISBN('1234');
$book->save();
BookTableMap::clearInstancePool();
$book = BookQuery::create()->findOneByTitle('Foo3');
$sb = serialize($book);
$this->assertEquals($book, unserialize($sb));
}
示例3: testMagicFindByObject
public function testMagicFindByObject()
{
$con = Propel::getServiceContainer()->getConnection(BookPeer::DATABASE_NAME);
$c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Author');
$testAuthor = $c->findOne();
$q = BookQuery::create()->findByAuthor($testAuthor);
$expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.AUTHOR_ID=" . $testAuthor->getId();
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByXXX($value) is turned into findBy(XXX, $value)');
$c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Author');
$testAuthor = $c->findOne();
$q = BookQuery::create()->findByAuthorAndISBN($testAuthor, 1234);
$expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` WHERE book.AUTHOR_ID=" . $testAuthor->getId() . " AND book.ISBN=1234";
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByXXXAndYYY($value) is turned into findBy(array(XXX, YYY), $value)');
}
示例4: testMagicRequireOneWithAndThrowsException
public function testMagicRequireOneWithAndThrowsException()
{
$this->setExpectedException('\\Propel\\Runtime\\Exception\\EntityNotFoundException', 'Book could not be found');
BookQuery::create()->requireOneByTitleAndId('Not Existing Book', -1337);
}
示例5: testDebugLog
public function testDebugLog()
{
$con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME);
// save data to return to normal state after test
$logger = $con->getLogger();
$logMethods = $con->getLogMethods();
$testLog = new Logger('debug');
$handler = new LastMessageHandler();
$testLog->pushHandler($handler);
$con->setLogger($testLog);
$con->setLogMethods(['exec', 'query', 'execute', 'beginTransaction', 'commit', 'rollBack']);
$con->useDebug(true);
$con->beginTransaction();
// test transaction log
$this->assertEquals('Begin transaction', $handler->latestMessage, 'PropelPDO logs begin transaction in debug mode');
$con->commit();
$this->assertEquals('Commit transaction', $handler->latestMessage, 'PropelPDO logs commit transaction in debug mode');
$con->beginTransaction();
$con->rollBack();
$this->assertEquals('Rollback transaction', $handler->latestMessage, 'PropelPDO logs rollback transaction in debug mode');
$con->beginTransaction();
$handler->latestMessage = '';
$con->beginTransaction();
$this->assertEquals('', $handler->latestMessage, 'PropelPDO does not log nested begin transaction in debug mode');
$con->commit();
$this->assertEquals('', $handler->latestMessage, 'PropelPDO does not log nested commit transaction in debug mode');
$con->beginTransaction();
$con->rollBack();
$this->assertEquals('', $handler->latestMessage, 'PropelPDO does not log nested rollback transaction in debug mode');
$con->rollback();
// test query log
$con->beginTransaction();
$c = new Criteria();
$c->add(BookTableMap::COL_TITLE, 'Harry%s', Criteria::LIKE);
$books = BookQuery::create(null, $c)->find($con);
$latestExecutedQuery = $this->getSql("SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id FROM book WHERE book.title LIKE 'Harry%s'");
$this->assertEquals($latestExecutedQuery, $handler->latestMessage, 'PropelPDO logs queries and populates bound parameters in debug mode');
BookTableMap::doDeleteAll($con);
$latestExecutedQuery = $this->getSql("DELETE FROM book");
$this->assertEquals($latestExecutedQuery, $handler->latestMessage, 'PropelPDO logs deletion queries in debug mode');
$latestExecutedQuery = 'DELETE FROM book WHERE 1=1';
$con->exec($latestExecutedQuery);
$this->assertEquals($latestExecutedQuery, $handler->latestMessage, 'PropelPDO logs exec queries in debug mode');
$con->commit();
// return to normal state after test
$con->setLogger($logger);
$con->setLogMethods($logMethods);
}
示例6: testGetResultsRespectsFormatter
public function testGetResultsRespectsFormatter()
{
$this->createBooks(5);
$query = BookQuery::create();
$query->setFormatter(ModelCriteria::FORMAT_ARRAY);
$pager = new PropelModelPager($query, 4);
$pager->setPage(1);
$pager->init();
$this->assertTrue($pager->getResults() instanceof ArrayCollection, 'getResults() returns a PropelArrayCollection if the query uses array hydration');
}
示例7: testGroupBy
public function testGroupBy()
{
$params = array();
$c = BookQuery::create()->joinReview()->withColumn('COUNT(Review.Id)', 'Count')->groupById();
$result = $c->createSelectSql($params);
if ($this->runningOnPostgreSQL()) {
$sql = 'SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, COUNT(review.ID) AS Count FROM book LEFT JOIN review ON (book.ID=review.BOOK_ID) GROUP BY book.ID,book.TITLE,book.ISBN,book.PRICE,book.PUBLISHER_ID,book.AUTHOR_ID';
} else {
$sql = $this->getSql('SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID, COUNT(review.ID) AS Count FROM `book` LEFT JOIN `review` ON (book.ID=review.BOOK_ID) GROUP BY book.ID');
}
$this->assertEquals($sql, $result);
}
示例8: testSubQueryCount
public function testSubQueryCount()
{
$subCriteria = new BookQuery();
$c = new BookQuery();
$c->addSelectQuery($subCriteria, 'subCriteriaAlias');
$c->filterByPrice(20, Criteria::LESS_THAN);
$nbBooks = $c->count();
$query = Propel::getConnection()->getLastExecutedQuery();
$sql = $this->getSql("SELECT COUNT(*) FROM (SELECT subCriteriaAlias.id, subCriteriaAlias.title, subCriteriaAlias.isbn, subCriteriaAlias.price, subCriteriaAlias.publisher_id, subCriteriaAlias.author_id FROM (SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id FROM book) AS subCriteriaAlias WHERE subCriteriaAlias.price<20) propelmatch4cnt");
$this->assertEquals($sql, $query, 'addSelectQuery() doCount is defined as complexQuery');
}
示例9: testCountableInterface
public function testCountableInterface()
{
BookQuery::create()->deleteAll();
$pager = $this->getPager(10);
$this->assertCount(0, $pager);
$this->createBooks(15);
$pager = $this->getPager(10);
$this->assertCount(10, $pager);
$pager = $this->getPager(10, 2);
$this->assertCount(5, $pager);
}
示例10: testGroupBy
public function testGroupBy()
{
$params = [];
$c = BookQuery::create()->joinReview()->withColumn('COUNT(Review.id)', 'Count')->groupById();
$result = $c->createSelectSql($params);
if ($this->runningOnPostgreSQL()) {
$sql = 'SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id, COUNT(review.id) AS Count FROM book LEFT JOIN review ON (book.id=review.book_id) GROUP BY book.id,book.title,book.isbn,book.price,book.publisher_id,book.author_id';
} else {
$sql = $this->getSql('SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id, COUNT(review.id) AS Count FROM book LEFT JOIN review ON (book.id=review.book_id) GROUP BY book.id');
}
$this->assertEquals($sql, $result);
}
示例11: testPopulateRelationManyToOne
public function testPopulateRelationManyToOne()
{
$con = Propel::getServiceContainer()->getReadConnection(BookTableMap::DATABASE_NAME);
AuthorTableMap::clearInstancePool();
BookTableMap::clearInstancePool();
$books = BookQuery::create()->find($con);
$count = $con->getQueryCount();
$books->populateRelation('Author', null, $con);
foreach ($books as $book) {
$author = $book->getAuthor();
}
$this->assertEquals($count + 1, $con->getQueryCount(), 'populateRelation() populates a many-to-one relationship with a single supplementary query');
}
示例12: testRemoveObjectOneToManyWithFkRequired
public function testRemoveObjectOneToManyWithFkRequired()
{
BookSummaryQuery::create()->deleteAll();
BookQuery::create()->deleteAll();
$bookSummary = new BookSummary();
$bookSummary->setSummary('summary Propel Book');
$bookSummary2 = new BookSummary();
$bookSummary2->setSummary('summary2 Propel Book');
$book = new Book();
$book->setTitle('Propel Book');
$book->setISBN('01234');
$book->addBookSummary($bookSummary);
$book->addBookSummary($bookSummary2);
$this->assertCount(2, $book->getBookSummaries());
$book->removeBookSummary($bookSummary);
$bookSummaries = $book->getBookSummaries();
$this->assertCount(1, $bookSummaries);
$this->assertEquals('summary2 Propel Book', $bookSummaries->getFirst()->getSummary());
$book->save();
$bookSummary2->save();
$this->assertEquals(1, BookQuery::create()->count(), 'One Book');
$this->assertEquals(1, BookSummaryQuery::create()->count(), 'One Summary');
$this->assertEquals(1, BookSummaryQuery::create()->filterBySummarizedBook($book)->count());
$book->addBookSummary($bookSummary);
$bookSummary->save();
$book->save();
$this->assertEquals(2, BookSummaryQuery::create()->filterBySummarizedBook($book)->count());
$book->removeBookSummary($bookSummary2);
$book->save();
$this->assertEquals(1, BookSummaryQuery::create()->filterBySummarizedBook($book)->count());
$this->assertEquals(1, BookSummaryQuery::create()->count(), 'One Book summary because FK is required so book summary is deleted when book is saved');
}
示例13: testMagicFindByObject
public function testMagicFindByObject()
{
$con = Propel::getServiceContainer()->getConnection(BookTableMap::DATABASE_NAME);
$c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Author');
$testAuthor = $c->findOne();
$q = BookQuery::create()->findByAuthor($testAuthor);
$expectedSQL = $this->getSql("SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id FROM book WHERE book.author_id=" . $testAuthor->getId());
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByXXX($value) is turned into findBy(XXX, $value)');
$c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Author');
$testAuthor = $c->findOne();
$q = BookQuery::create()->findByAuthorAndISBN($testAuthor, 1234);
$expectedSQL = $this->getSql("SELECT book.id, book.title, book.isbn, book.price, book.publisher_id, book.author_id FROM book WHERE book.author_id=" . $testAuthor->getId() . " AND book.isbn=1234");
$this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'findByXXXAndYYY($value) is turned into findBy(array(XXX, YYY), $value)');
}
示例14: testSetterOneToManyReplacesOldObjectsByNewObjects
public function testSetterOneToManyReplacesOldObjectsByNewObjects()
{
// Ensure no data
BookQuery::create()->deleteAll();
AuthorQuery::create()->deleteAll();
$books = new ObjectCollection();
foreach (array('foo', 'bar') as $title) {
$b = new Book();
$b->setTitle($title);
$books[] = $b;
}
$a = new Author();
$a->setBooks($books);
$a->save();
$books = $a->getBooks();
$this->assertEquals('foo', $books[0]->getTitle());
$this->assertEquals('bar', $books[1]->getTitle());
$books = new ObjectCollection();
foreach (array('bam', 'bom') as $title) {
$b = new Book();
$b->setTitle($title);
$books[] = $b;
}
$a->setBooks($books);
$a->save();
$books = $a->getBooks();
$this->assertEquals('bam', $books[0]->getTitle());
$this->assertEquals('bom', $books[1]->getTitle());
$this->assertEquals(1, AuthorQuery::create()->count());
$this->assertEquals(2, BookQuery::create()->count());
}
示例15: testLobSetting_WriteMode
public function testLobSetting_WriteMode()
{
$blob_path = $this->getLobFile('tin_drum.gif');
$blob2_path = $this->getLobFile('propel.gif');
$clob_path = $this->getLobFile('tin_drum.txt');
$book = BookQuery::create()->findOne();
$m1 = new Media();
$m1->setBook($book);
$m1->setCoverImage(file_get_contents($blob_path));
$m1->setExcerpt(file_get_contents($clob_path));
$m1->save();
MediaTableMap::clearInstancePool();
// make sure we have the latest from the db:
$m2 = MediaQuery::create()->findPk($m1->getId());
// now attempt to assign a temporary stream, opened in 'w' mode, to the db
$stream = fopen("php://memory", 'w');
fwrite($stream, file_get_contents($blob2_path));
$m2->setCoverImage($stream);
$m2->save();
fclose($stream);
$m2->reload();
$this->assertEquals(md5(file_get_contents($blob2_path)), md5(stream_get_contents($m2->getCoverImage())), "Expected contents to match when setting stream w/ 'w' mode");
$stream2 = fopen("php://memory", 'w+');
fwrite($stream2, file_get_contents($blob_path));
rewind($stream2);
$this->assertEquals(md5(file_get_contents($blob_path)), md5(stream_get_contents($stream2)), "Expecting setup to be correct");
$m2->setCoverImage($stream2);
$m2->save();
$m2->reload();
$this->assertEquals(md5(file_get_contents($blob_path)), md5(stream_get_contents($m2->getCoverImage())), "Expected contents to match when setting stream w/ 'w+' mode");
}