Esempio n. 1
0
 public function testFindOneWithDistantClass()
 {
     BookstoreDataPopulator::populate();
     BookPeer::clearInstancePool();
     AuthorPeer::clearInstancePool();
     ReviewPeer::clearInstancePool();
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Review');
     $c->setFormatter(ModelCriteria::FORMAT_ARRAY);
     $c->where('Propel\\Tests\\Bookstore\\Review.Recommended = ?', true);
     $c->join('Propel\\Tests\\Bookstore\\Review.Book');
     $c->with('Book');
     $c->join('Book.Author');
     $c->with('Author');
     $review = $c->findOne();
     $this->assertEquals($review['ReviewedBy'], 'Washington Post', 'Main object is correctly hydrated');
     $book = $review['Book'];
     $this->assertEquals('Harry Potter and the Order of the Phoenix', $book['Title'], 'Related object is correctly hydrated');
     $author = $book['Author'];
     $this->assertEquals('J.K.', $author['FirstName'], 'Related object is correctly hydrated');
 }
Esempio n. 2
0
 public function testFindOneWithDistantClass()
 {
     BookstoreDataPopulator::populate();
     BookPeer::clearInstancePool();
     AuthorPeer::clearInstancePool();
     ReviewPeer::clearInstancePool();
     Propel::enableInstancePooling();
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Review');
     $c->where('Propel\\Tests\\Bookstore\\Review.Recommended = ?', true);
     $c->join('Propel\\Tests\\Bookstore\\Review.Book');
     $c->with('Book');
     $c->join('Book.Author');
     $c->with('Author');
     $con = Propel::getServiceContainer()->getConnection(BookPeer::DATABASE_NAME);
     $review = $c->findOne($con);
     $count = $con->getQueryCount();
     $this->assertEquals($review->getReviewedBy(), 'Washington Post', 'Main object is correctly hydrated');
     $book = $review->getBook();
     $this->assertEquals($count, $con->getQueryCount(), 'with() hydrates the related objects to save a query');
     $this->assertEquals('Harry Potter and the Order of the Phoenix', $book->getTitle(), 'Related object is correctly hydrated');
     $author = $book->getAuthor();
     $this->assertEquals($count, $con->getQueryCount(), 'with() hydrates the related objects to save a query');
     $this->assertEquals('J.K.', $author->getFirstName(), 'Related object is correctly hydrated');
 }
 public function testSelectArrayJoin()
 {
     BookstoreDataPopulator::depopulate($this->con);
     BookstoreDataPopulator::populate($this->con);
     $c = new ModelCriteria('bookstore', '\\Propel\\Tests\\Bookstore\\Book');
     $c->join('Book.Author');
     $c->where('Author.FirstName = ?', 'Neal');
     $c->select(array('Title', 'ISBN'));
     $titles = $c->find($this->con);
     $this->assertEquals($titles->count(), 1, 'find() called after select(array) allows for join() statements');
     $expectedSQL = "SELECT book.TITLE AS \"Title\", book.ISBN AS \"ISBN\" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal'";
     $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(array) allows for join() statements');
     $c = new ModelCriteria('bookstore', '\\Propel\\Tests\\Bookstore\\Book');
     $c->join('Book.Author');
     $c->where('Author.FirstName = ?', 'Neal');
     $c->select(array('Author.FirstName', 'Author.LastName'));
     $titles = $c->find($this->con);
     $this->assertEquals(array_values($titles->shift()), array('Neal', 'Stephenson'), 'find() called after select(array) will return values from the joined table using complete column names');
     $c = new ModelCriteria('bookstore', '\\Propel\\Tests\\Bookstore\\Book');
     $c->join('Book.Author');
     $c->where('Author.FirstName = ?', 'Neal');
     $c->select(array('Title', 'ISBN'));
     $title = $c->findOne($this->con);
     $this->assertEquals(count($title), 2, 'findOne() called after select(array) allows for join() statements');
     $expectedSQL = "SELECT book.TITLE AS \"Title\", book.ISBN AS \"ISBN\" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Neal' LIMIT 1";
     $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'findOne() called after select(array) allows for join() statements');
     $c = new ModelCriteria('bookstore', '\\Propel\\Tests\\Bookstore\\Book');
     $c->join('Book.Author');
     $c->where('Author.FirstName = ?', 'Neal');
     $c->select(array('Author.FirstName', 'Author.LastName'));
     $title = $c->findOne($this->con);
     $this->assertEquals(array_values($title), array('Neal', 'Stephenson'), 'findOne() called after select(array) will return values from the joined table using complete column names');
 }
Esempio n. 4
0
 public function testUseQueryCustomClass()
 {
     $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book', 'b');
     $c->thisIsMe = true;
     $c->where('b.Title = ?', 'foo');
     $c->setLimit(10);
     $c->leftJoin('b.Author a');
     $c2 = $c->useQuery('a', 'Propel\\Tests\\Runtime\\Query\\ModelCriteriaForUseQuery');
     $this->assertTrue($c2 instanceof ModelCriteriaForUseQuery, 'useQuery() returns a secondary Criteria with the custom class');
     $c2->withNoName();
     $c = $c2->endUse();
     $con = Propel::getServiceContainer()->getConnection(BookPeer::DATABASE_NAME);
     $c->find($con);
     $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE book.TITLE = 'foo' AND a.FIRST_NAME IS NOT NULL  AND a.LAST_NAME IS NOT NULL LIMIT 10";
     $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'useQuery() and endUse() allow to merge a custom secondary criteria');
 }