public function testPreAndPostDelete() { $c = new ModelCriteria('bookstore', '\\Propel\\Tests\\Bookstore\\Book'); $books = $c->find(); $count = count($books); $book = $books->shift(); $this->con->lastAffectedRows = 0; $c = new ModelCriteriaWithPreAndPostDeleteHook('bookstore', '\\Propel\\Tests\\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', '\\Propel\\Tests\\Bookstore\\Book'); $nbBooks = $c->deleteAll($this->con); $this->assertEquals(12, $this->con->lastAffectedRows, 'postDelete() is called after deleteAll() even if preDelete() returns not null'); }
public function testFindWithLeftJoinWithManyToOneAndNullObject() { BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $review = new Review(); $review->save($this->con); $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Review'); $c->setFormatter(ModelCriteria::FORMAT_ARRAY); $c->leftJoinWith('Propel\\Tests\\Bookstore\\Review.Book'); $c->leftJoinWith('Book.Author'); // should not raise a notice $reviews = $c->find($this->con); $this->assertTrue(true); }
public function testSelectArrayWithColumn() { BookstoreDataPopulator::depopulate($this->con); BookstoreDataPopulator::populate($this->con); $c = new ModelCriteria('bookstore', '\\Propel\\Tests\\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()'); }
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', 'Propel\\Tests\\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 testFindOneWithOneToManyThenManyToOneUsingAlias() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Author'); $c->add(AuthorPeer::LAST_NAME, 'Rowling'); $c->leftJoinWith('Propel\\Tests\\Bookstore\\Author.Book b'); $c->leftJoinWith('b.Review r'); $con = Propel::getServiceContainer()->getConnection(BookPeer::DATABASE_NAME); $authors = $c->find($con); $this->assertEquals(1, count($authors), 'with() does not duplicate the main object'); $rowling = $authors[0]; $count = $con->getQueryCount(); $this->assertEquals($rowling->getFirstName(), 'J.K.', 'Main object is correctly hydrated'); $books = $rowling->getBooks(); $this->assertEquals($count, $con->getQueryCount(), 'with() hydrates the related objects to save a query '); $this->assertEquals(1, count($books), 'Related objects are correctly hydrated'); $book = $books[0]; $this->assertEquals($book->getTitle(), 'Harry Potter and the Order of the Phoenix', 'Related object is correctly hydrated'); $reviews = $book->getReviews(); $this->assertEquals($count, $con->getQueryCount(), 'with() hydrates the related objects to save a query '); $this->assertEquals(2, count($reviews), 'Related objects are correctly hydrated'); }
public function testUpdateOneByOne() { $con = Propel::getServiceContainer()->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', 'Propel\\Tests\\Bookstore\\Book'); $books = $c->find(); foreach ($books as $book) { $book->save(); } $count = $con->getQueryCount(); $c = new ModelCriteria('bookstore', 'Propel\\Tests\\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', 'Propel\\Tests\\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', 'Propel\\Tests\\Bookstore\\Book'); $books = $c->find(); foreach ($books as $book) { $book->save(); } $count = $con->getQueryCount(); $c = new ModelCriteria('bookstore', 'Propel\\Tests\\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', 'Propel\\Tests\\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 testFindOneWithClassAndColumn() { BookstoreDataPopulator::populate(); BookPeer::clearInstancePool(); AuthorPeer::clearInstancePool(); ReviewPeer::clearInstancePool(); $c = new ModelCriteria('bookstore', 'Propel\\Tests\\Bookstore\\Book'); $c->setFormatter(ModelCriteria::FORMAT_ON_DEMAND); $c->filterByTitle('The Tin Drum'); $c->join('Propel\\Tests\\Bookstore\\Book.Author'); $c->withColumn('Author.FirstName', 'AuthorName'); $c->withColumn('Author.LastName', 'AuthorName2'); $c->with('Author'); $c->limit(1); $con = Propel::getServiceContainer()->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, 'ObjectFormatter correctly hydrates with class'); $this->assertEquals('Gunter', $book->getAuthor()->getFirstName(), 'ObjectFormatter correctly hydrates with class'); $this->assertEquals('Gunter', $book->getVirtualColumn('AuthorName'), 'ObjectFormatter adds withColumns as virtual columns'); $this->assertEquals('Grass', $book->getVirtualColumn('AuthorName2'), 'ObjectFormatter correctly hydrates all virtual columns'); }