/** * {@inheritDoc} */ public function findBy(array $criteria) { $this->source->clear(); foreach ($criteria as $field => $value) { $method = 'filterBy' . ucfirst($field); $this->source->{$method}($value); } return $this->source->findOne(); }
public function testFindOneWithClassAndColumn() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $c = new ModelCriteria('bookstore', 'Book'); $c->setFormatter(ModelCriteria::FORMAT_ARRAY); $c->filterByTitle('The Tin Drum'); $c->join('Book.Author'); $c->withColumn('Author.FirstName', 'AuthorName'); $c->withColumn('Author.LastName', 'AuthorName2'); $c->with('Author'); $con = Propel::getConnection(BookPeer::DATABASE_NAME); $book = $c->findOne($con); $this->assertEquals(array('Id', 'Title', 'ISBN', 'Price', 'PublisherId', 'AuthorId', 'Author', 'AuthorName', 'AuthorName2'), array_keys($book), 'withColumn() do not change the resulting model class'); $this->assertEquals('The Tin Drum', $book['Title']); $this->assertEquals('Gunter', $book['Author']['FirstName'], 'PropelArrayFormatter correctly hydrates withclass and columns'); $this->assertEquals('Gunter', $book['AuthorName'], 'PropelArrayFormatter adds withColumns as columns'); $this->assertEquals('Grass', $book['AuthorName2'], 'PropelArrayFormatter correctly hydrates all as columns'); }
public function testFindOneWithClassAndColumn() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $c = new ModelCriteria('bookstore', 'Book'); $c->filterByTitle('The Tin Drum'); $c->join('Book.Author'); $c->withColumn('Author.FirstName', 'AuthorName'); $c->withColumn('Author.LastName', 'AuthorName2'); $c->with('Author'); $con = Propel::getConnection(BookPeer::DATABASE_NAME); $book = $c->findOne($con); $this->assertTrue($book instanceof Book, 'withColumn() do not change the resulting model class'); $this->assertEquals('The Tin Drum', $book->getTitle()); $this->assertTrue($book->getAuthor() instanceof Author, 'PropelObjectFormatter correctly hydrates with class'); $this->assertEquals('Gunter', $book->getAuthor()->getFirstName(), 'PropelObjectFormatter correctly hydrates with class'); $this->assertEquals('Gunter', $book->getVirtualColumn('AuthorName'), 'PropelObjectFormatter adds withColumns as virtual columns'); $this->assertEquals('Grass', $book->getVirtualColumn('AuthorName2'), 'PropelObjectFormatter correctly hydrates all virtual columns'); }
public function testFindOneWithDistantClassRenamedRelation() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); Propel::enableInstancePooling(); $c = new ModelCriteria('bookstore', 'BookSummary'); $c->joinWith('BookSummary.SummarizedBook'); $c->joinWith('SummarizedBook.Author'); $c->setFormatter(ModelCriteria::FORMAT_ON_DEMAND); $con = Propel::getConnection(BookPeer::DATABASE_NAME); $summary = $c->findOne($con); $count = $con->getQueryCount(); $this->assertEquals('Harry Potter does some amazing magic!', $summary->getSummary(), 'Main object is correctly hydrated'); $book = $summary->getSummarizedBook(); $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 testMagicJoin() { $con = Propel::getConnection(BookPeer::DATABASE_NAME); $c = new ModelCriteria('bookstore', 'Book', 'b'); $c->leftJoin('b.Author a'); $c->where('a.FirstName = ?', 'Leo'); $books = $c->findOne($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 a.FIRST_NAME = 'Leo' LIMIT 1"; $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'leftJoin($x) is turned into join($x, Criteria::LEFT_JOIN)'); $books = BookQuery::create()->leftJoinAuthor('a')->where('a.FirstName = ?', 'Leo')->findOne($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 a.FIRST_NAME = 'Leo' LIMIT 1"; $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'leftJoinX() is turned into join($x, Criteria::LEFT_JOIN)'); $c = new ModelCriteria('bookstore', 'Book', 'b'); $c->innerJoin('b.Author a'); $c->where('a.FirstName = ?', 'Leo'); $books = $c->findOne($con); $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"; $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'innerJoin($x) is turned into join($x, Criteria::INNER_JOIN)'); $books = BookQuery::create()->innerJoinAuthor('a')->where('a.FirstName = ?', 'Leo')->findOne($con); $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` INNER JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"; $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'innerJoinX() is turned into join($x, Criteria::INNER_JOIN)'); $c = new ModelCriteria('bookstore', 'Book', 'b'); $c->rightJoin('b.Author a'); $c->where('a.FirstName = ?', 'Leo'); $books = $c->findOne($con); $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` RIGHT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"; $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'rightJoin($x) is turned into join($x, Criteria::RIGHT_JOIN)'); $books = BookQuery::create()->rightJoinAuthor('a')->where('a.FirstName = ?', 'Leo')->findOne($con); $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` RIGHT JOIN `author` `a` ON (book.AUTHOR_ID=a.ID) WHERE a.FIRST_NAME = 'Leo' LIMIT 1"; $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'rightJoinX() is turned into join($x, Criteria::RIGHT_JOIN)'); $books = BookQuery::create()->leftJoinAuthor()->where('Author.FirstName = ?', 'Leo')->findOne($con); $expectedSQL = "SELECT book.ID, book.TITLE, book.ISBN, book.PRICE, book.PUBLISHER_ID, book.AUTHOR_ID FROM `book` LEFT JOIN `author` ON (book.AUTHOR_ID=author.ID) WHERE author.FIRST_NAME = 'Leo' LIMIT 1"; $this->assertEquals($expectedSQL, $con->getLastExecutedQuery(), 'leftJoinX() is turned into join($x, Criteria::LEFT_JOIN)'); }
public function testFilterByPrimaryKeyCompositeKey() { BookstoreDataPopulator::depopulate(); BookstoreDataPopulator::populate(); // save all books to make sure related objects are also saved - BookstoreDataPopulator keeps some unsaved $c = new ModelCriteria('bookstore', 'Book'); $books = $c->find(); foreach ($books as $book) { $book->save(); } BookPeer::clearInstancePool(); // retrieve the test data $c = new ModelCriteria('bookstore', 'BookListRel'); $bookListRelTest = $c->findOne(); $pk = $bookListRelTest->getPrimaryKey(); $q = new BookListRelQuery(); $q->filterByPrimaryKey($pk); $q1 = BookListRelQuery::create()->add(BookListRelPeer::BOOK_ID, $pk[0], Criteria::EQUAL)->add(BookListRelPeer::BOOK_CLUB_LIST_ID, $pk[1], Criteria::EQUAL); $this->assertEquals($q1, $q, 'filterByPrimaryKey() translates to a Criteria::EQUAL in the PK columns'); }
public function testMagicFindByObject() { $con = Propel::getConnection(BookPeer::DATABASE_NAME); $c = new ModelCriteria('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', '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)'); }
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 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); }