public function testExplainPlanFromObject() { BookstoreDataPopulator::depopulate($this->con); BookstoreDataPopulator::populate($this->con); $db = Propel::getDb(BookPeer::DATABASE_NAME); $c = new ModelCriteria('bookstore', 'Book'); $c->join('Book.Author'); $c->where('Author.FirstName = ?', 'Neal'); $c->select('Title'); $explain = $c->explain($this->con); if ($db instanceof DBMySQL) { $this->assertEquals(sizeof($explain), 2, 'Explain plan return two lines'); // explain can change sometime, test can't be strict $this->assertArrayHasKey('select_type', $explain[0], 'Line 1, select_type key exist'); $this->assertArrayHasKey('table', $explain[0], 'Line 1, table key exist'); $this->assertArrayHasKey('type', $explain[0], 'Line 1, type key exist'); $this->assertArrayHasKey('possible_keys', $explain[0], 'Line 1, possible_keys key exist'); $this->assertArrayHasKey('select_type', $explain[1], 'Line 2, select_type key exist'); $this->assertArrayHasKey('table', $explain[1], 'Line 2, table key exist'); $this->assertArrayHasKey('type', $explain[1], 'Line 2, type key exist'); $this->assertArrayHasKey('possible_keys', $explain[1], 'Line 2, possible_keys key exist'); } elseif ($db instanceof DBOracle) { $this->assertTrue(sizeof($explain) > 2, 'Explain plan return more than 2 lines'); } else { $this->markTestSkipped('Cannot test explain plan on adapter ' . get_class($db)); } }
public function testFindOneWithDistantClass() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $c = new ModelCriteria('bookstore', 'Review'); $c->setFormatter(ModelCriteria::FORMAT_ARRAY); $c->where('Review.Recommended = ?', true); $c->join('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'); }
public function testFindOneWithDistantClass() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); Propel::enableInstancePooling(); $c = new ModelCriteria('bookstore', 'Review'); $c->where('Review.Recommended = ?', true); $c->join('Review.Book'); $c->with('Book'); $c->join('Book.Author'); $c->with('Author'); $con = Propel::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 testUseQueryCustomClass() { $c = new ModelCriteria('bookstore', 'Book', 'b'); $c->thisIsMe = true; $c->where('b.Title = ?', 'foo'); $c->setLimit(10); $c->leftJoin('b.Author a'); $c2 = $c->useQuery('a', 'ModelCriteriaForUseQuery'); $this->assertTrue($c2 instanceof ModelCriteriaForUseQuery, 'useQuery() returns a secondary Criteria with the custom class'); $c2->withNoName(); $c = $c2->endUse(); $con = Propel::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'); }
public function testSelectArrayJoin() { BookstoreDataPopulator::depopulate($this->con); BookstoreDataPopulator::populate($this->con); $c = new ModelCriteria('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', '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', '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', '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'); }
public function testExistsWithNonExistentCondition() { $c = new ModelCriteria('bookstore', 'Book', 'b'); $c->where('b.Title = ?', 'jenexistepas'); $this->assertFalse($c->exists()); }
protected function filterByKeyword(\ModelCriteria $query, $params) { if (!empty($params['q'])) { $columns = array(); $peerClass = constant($this->query->getModelName() . '::PEER'); $phpNames = call_user_func(array($peerClass, 'getFieldNames')); foreach ($this->columns as $name => $column) { if (!empty($column['phpName'])) { if (in_array($column['phpName'], $phpNames)) { $columns[] = $this->query->getModelName() . '.' . $column['phpName']; } } } array_unshift($columns, '"|"'); $query->where('CONCAT_WS(' . join(',', $columns) . ') LIKE ?', '%' . $params['q'] . '%'); } }
public function testQuotingAliases() { $c = new ModelCriteria('bookstore', 'Book'); $c->withColumn('book.title', 'Book Title (*.)'); $c->select('Book Title (*.)'); $c->where('Book.Isbn = ?', '0380977427'); $title = $c->findOne(); $expectedSQL = "SELECT book.title AS `Book Title (*.)` FROM `book` WHERE book.isbn = '0380977427' LIMIT 1"; $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery()); $this->assertEquals('Quicksilver', $title); }