Esempio n. 1
0
 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');
 }