/** * Execute query, store result and return it. * * @return PropelCollection */ protected function getCollection() { if ($this->collection === null) { $this->collection = $this->query->find(); } return $this->collection; }
public function testPreAndPostDelete() { $c = new ModelCriteria('bookstore', 'Book'); $books = $c->find(); $count = count($books); $book = $books->shift(); $this->con->lastAffectedRows = 0; $c = new ModelCriteriaWithPreAndPostDeleteHook('bookstore', 'Book', 'b'); $c->where('b.Id = ?', $book->getId()); $nbBooks = $c->delete($this->con); $this->assertEquals(12, $this->con->lastAffectedRows, 'postDelete() is called after delete() even if preDelete() returns not null'); $this->con->lastAffectedRows = 0; $c = new ModelCriteriaWithPreAndPostDeleteHook('bookstore', 'Book'); $nbBooks = $c->deleteAll($this->con); $this->assertEquals(12, $this->con->lastAffectedRows, 'postDelete() is called after deleteAll() even if preDelete() returns not null'); }
/** * Loads the complete choice list entries, once. * * If data has been loaded the choice list is initialized with the retrieved data. */ private function load() { if ($this->loaded) { return; } $models = (array) $this->query->find(); $preferred = array(); if ($this->preferredQuery instanceof \ModelCriteria) { $preferred = (array) $this->preferredQuery->find(); } try { // The second parameter $labels is ignored by ObjectChoiceList parent::initialize($models, array(), $preferred); $this->loaded = true; } catch (StringCastException $e) { throw new StringCastException(str_replace('argument $labelPath', 'option "property"', $e->getMessage()), null, $e); } }
public function testFindWithLeftJoinWithManyToOneAndNullObject() { BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $review = new Review(); $review->save($this->con); $c = new ModelCriteria('bookstore', 'Review'); $c->setFormatter(ModelCriteria::FORMAT_ARRAY); $c->leftJoinWith('Review.Book'); $c->leftJoinWith('Book.Author'); // should not raise a notice $reviews = $c->find($this->con); $this->assertTrue(true); }
public function testFindOneWithOneToManyThenManyToOneUsingAlias() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $c = new ModelCriteria('bookstore', 'Author'); $c->add(AuthorPeer::LAST_NAME, 'Rowling'); $c->leftJoinWith('Author.Book b'); $c->leftJoinWith('b.Review r'); $c->setFormatter(ModelCriteria::FORMAT_ARRAY); $con = Propel::getConnection(BookPeer::DATABASE_NAME); $authors = $c->find($con); $this->assertEquals(1, count($authors), 'with() does not duplicate the main object'); $rowling = $authors[0]; $this->assertEquals($rowling['FirstName'], 'J.K.', 'Main object is correctly hydrated'); $books = $rowling['Books']; $this->assertEquals(1, count($books), 'Related objects are correctly hydrated'); $book = $books[0]; $this->assertEquals($book['Title'], 'Harry Potter and the Order of the Phoenix', 'Related object is correctly hydrated'); $reviews = $book['Reviews']; $this->assertEquals(2, count($reviews), 'Related objects are correctly hydrated'); }
public function testFindOneWithClassAndColumn() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $c = new ModelCriteria('bookstore', 'Book'); $c->setFormatter(ModelCriteria::FORMAT_ON_DEMAND); $c->filterByTitle('The Tin Drum'); $c->join('Book.Author'); $c->withColumn('Author.FirstName', 'AuthorName'); $c->withColumn('Author.LastName', 'AuthorName2'); $c->with('Author'); $c->limit(1); $con = Propel::getConnection(BookPeer::DATABASE_NAME); $books = $c->find($con); foreach ($books as $book) { break; } $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 testUpdateOneByOne() { $con = Propel::getConnection(BookPeer::DATABASE_NAME); BookstoreDataPopulator::depopulate($con); BookstoreDataPopulator::populate($con); // 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(); } $count = $con->getQueryCount(); $c = new ModelCriteria('bookstore', 'Book'); $nbBooks = $c->update(array('Title' => 'foo'), $con, true); $this->assertEquals(4, $nbBooks, 'update() returns the number of updated rows'); $this->assertEquals($count + 1 + 4, $con->getQueryCount(), 'update() updates the objects one by one when called with true as last parameter'); $c = new ModelCriteria('bookstore', 'Book', 'b'); $c->where('b.Title = ?', 'foo'); $nbBooks = $c->count(); $this->assertEquals(4, $nbBooks, 'update() updates all records by default'); BookstoreDataPopulator::depopulate($con); BookstoreDataPopulator::populate($con); // 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(); } $count = $con->getQueryCount(); $c = new ModelCriteria('bookstore', 'Book', 'b'); $c->where('b.Title = ?', 'Don Juan'); $nbBooks = $c->update(array('ISBN' => '3456'), $con, true); $this->assertEquals(1, $nbBooks, 'update() updates only the records matching the criteria'); $this->assertEquals($count + 1 + 1, $con->getQueryCount(), 'update() updates the objects one by one when called with true as last parameter'); $c = new ModelCriteria('bookstore', 'Book', 'b'); $c->where('b.Title = ?', 'Don Juan'); $book = $c->findOne(); $this->assertEquals('3456', $book->getISBN(), 'update() updates only the records matching the criteria'); }
public function testPruneCompositeKey() { 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(); $nbBookListRel = BookListRelQuery::create()->prune()->count(); $this->assertEquals(2, $nbBookListRel, 'prune() does nothing when passed a null object'); $testBookListRel = BookListRelQuery::create()->findOne(); $nbBookListRel = BookListRelQuery::create()->prune($testBookListRel)->count(); $this->assertEquals(1, $nbBookListRel, 'prune() removes an object from the result'); }
public function testSelectArrayWithColumn() { BookstoreDataPopulator::depopulate($this->con); BookstoreDataPopulator::populate($this->con); $c = new ModelCriteria('bookstore', 'Book'); $c->join('Book.Author'); $c->withColumn('LOWER(Book.Title)', 'LowercaseTitle'); $c->select(array('LowercaseTitle', 'Book.Title')); $c->orderBy('Book.Title'); $rows = $c->find($this->con); $expectedSQL = 'SELECT LOWER(book.TITLE) AS LowercaseTitle, book.TITLE AS "Book.Title" FROM `book` INNER JOIN `author` ON (book.AUTHOR_ID=author.ID) ORDER BY book.TITLE ASC'; $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(array) can cope with a column added with withColumn()'); $expectedRows = array(array('LowercaseTitle' => 'don juan', 'Book.Title' => 'Don Juan'), array('LowercaseTitle' => 'harry potter and the order of the phoenix', 'Book.Title' => 'Harry Potter and the Order of the Phoenix'), array('LowercaseTitle' => 'quicksilver', 'Book.Title' => 'Quicksilver'), array('LowercaseTitle' => 'the tin drum', 'Book.Title' => 'The Tin Drum')); $this->assertEquals(serialize($rows->getData()), serialize($expectedRows), 'find() called after select(array) can cope with a column added with withColumn()'); }
/** * {@inheritDoc} */ public function findAll() { $this->source->clear(); return $this->source->find(); }
public function testSelectArrayWithColumnOrder() { BookstoreDataPopulator::depopulate($this->con); BookstoreDataPopulator::populate($this->con); $c = new ModelCriteria('bookstore', 'Book'); $c->join('Book.Author'); $c->withColumn('LOWER(Book.Title)', 'LowercaseTitle'); $c->withColumn('UPPER(Book.Title)', 'UppercaseTitle'); $c->select(array('Book.ISBN', 'LowercaseTitle', 'Book.Title', 'UppercaseTitle')); $c->orderBy('Book.Title'); $rows = $c->find($this->con); $expectedSQL = 'SELECT book.isbn AS "Book.ISBN", LOWER(book.title) AS LowercaseTitle, book.title AS "Book.Title", UPPER(book.title) AS UppercaseTitle FROM `book` INNER JOIN `author` ON (book.author_id=author.id) ORDER BY book.title ASC'; $this->assertEquals($expectedSQL, $this->con->getLastExecutedQuery(), 'find() called after select(array) can cope with a column added with withColumn()'); $expectedRows = array(array('Book.ISBN' => '0140422161', 'LowercaseTitle' => 'don juan', 'Book.Title' => 'Don Juan', 'UppercaseTitle' => 'DON JUAN'), array('Book.ISBN' => '043935806X', 'LowercaseTitle' => 'harry potter and the order of the phoenix', 'Book.Title' => 'Harry Potter and the Order of the Phoenix', 'UppercaseTitle' => 'HARRY POTTER AND THE ORDER OF THE PHOENIX'), array('Book.ISBN' => '0380977427', 'LowercaseTitle' => 'quicksilver', 'Book.Title' => 'Quicksilver', 'UppercaseTitle' => 'QUICKSILVER'), array('Book.ISBN' => '067972575X', 'LowercaseTitle' => 'the tin drum', 'Book.Title' => 'The Tin Drum', 'UppercaseTitle' => 'THE TIN DRUM')); $this->assertEquals(serialize($rows->getData()), serialize($expectedRows), 'find() called after select(array) can cope with a column added with withColumn()'); }