public function testDoUpdate() { try { $c1 = new Criteria(); $c1->setPrimaryTableName(BookPeer::TABLE_NAME); $c1->add(BookPeer::ID, 12, ' BAD SQL'); $c2 = new Criteria(); $c2->add(BookPeer::TITLE, 'Foo'); BasePeer::doUpdate($c1, $c2, Propel::getServiceContainer()->getWriteConnection(BookPeer::DATABASE_NAME)); } catch (RuntimeException $e) { $this->assertContains('[UPDATE `book` SET `TITLE`=:p1 WHERE book.ID BAD SQL:p2]', $e->getMessage(), 'SQL query is written in the exception message'); } }
public function testCommentDoUpdate() { $c1 = new Criteria(); $c1->setPrimaryTableName(BookPeer::TABLE_NAME); $c1->setComment('Foo'); $c2 = new Criteria(); $c2->add(BookPeer::TITLE, 'Updated Title'); $con = Propel::getServiceContainer()->getConnection(BookPeer::DATABASE_NAME); BasePeer::doUpdate($c1, $c2, $con); $expected = 'UPDATE /* Foo */ `book` SET `TITLE`=\'Updated Title\''; $this->assertEquals($expected, $con->getLastExecutedQuery(), 'Criteria::setComment() adds a comment to update queries'); }
/** * Issue an UPDATE query based the current ModelCriteria and a list of changes. * This method is called by ModelCriteria::update() inside a transaction. * * @param array $values Associative array of keys and values to replace * @param ConnectionInterface $con a connection object * @param boolean $forceIndividualSaves If false (default), the resulting call is a BasePeer::doUpdate(), ortherwise it is a series of save() calls on all the found objects * * @return Integer Number of updated rows */ public function doUpdate($values, $con, $forceIndividualSaves = false) { if ($forceIndividualSaves) { // Update rows one by one $objects = $this->setFormatter(ModelCriteria::FORMAT_OBJECT)->find($con); foreach ($objects as $object) { foreach ($values as $key => $value) { $object->setByName($key, $value); } } $objects->save($con); $affectedRows = count($objects); } else { // update rows in a single query $set = new Criteria($this->getDbName()); foreach ($values as $columnName => $value) { $realColumnName = $this->getTableMap()->getColumnByPhpName($columnName)->getFullyQualifiedName(); $set->add($realColumnName, $value); } $affectedRows = BasePeer::doUpdate($this, $set, $con); call_user_func(array($this->modelPeerName, 'clearInstancePool')); call_user_func(array($this->modelPeerName, 'clearRelatedInstancePool')); } return $affectedRows; }