public function testPreDelete() { $c = new ModelCriteria('bookstore', 'Book'); $books = $c->find(); $count = count($books); $book = $books->shift(); $c = new ModelCriteriaWithPreDeleteHook('bookstore', 'Book', 'b'); $c->where('b.Id = ?', $book->getId()); $nbBooks = $c->delete(); $this->assertEquals(12, $nbBooks, 'preDelete() can change the return value of delete()'); $c = new ModelCriteria('bookstore', 'Book'); $nbBooks = $c->count(); $this->assertEquals($count, $nbBooks, 'preDelete() can bypass the row deletion'); $c = new ModelCriteriaWithPreDeleteHook('bookstore', 'Book'); $nbBooks = $c->deleteAll(); $this->assertEquals(12, $nbBooks, 'preDelete() can change the return value of deleteAll()'); $c = new ModelCriteria('bookstore', 'Book'); $nbBooks = $c->count(); $this->assertEquals($count, $nbBooks, 'preDelete() can bypass the row deletion'); }
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'); }