DboSource::update PHP Method

update() public method

For databases that do not support aliases in UPDATE queries.
public update ( Model $Model, array $fields = [], array $values = null, mixed $conditions = null ) : boolean
$Model Model The model to update.
$fields array The fields to update
$values array The values fo the fields.
$conditions mixed The conditions for the update. When non-empty $values will not be quoted.
return boolean Success
    public function update(Model $Model, $fields = array(), $values = null, $conditions = null)
    {
        if (!$values) {
            $combined = $fields;
        } else {
            $combined = array_combine($fields, $values);
        }
        $fields = implode(', ', $this->_prepareUpdateFields($Model, $combined, empty($conditions)));
        $alias = $joins = null;
        $table = $this->fullTableName($Model);
        $conditions = $this->_matchRecords($Model, $conditions);
        if ($conditions === false) {
            return false;
        }
        $query = compact('table', 'alias', 'joins', 'fields', 'conditions');
        if (!$this->execute($this->renderStatement('update', $query))) {
            $Model->onError();
            return false;
        }
        return true;
    }

Usage Example

Example #1
0
/**
 * testStatements method
 *
 * @return void
 */
	public function testUpdateStatements() {
		$this->loadFixtures('Article', 'User');
		$test = ConnectionManager::getDatasource('test');
		$db = $test->config['database'];

		$this->Dbo = $this->getMock('Mysql', array('execute'), array($test->config));

		$this->Dbo->expects($this->at(0))->method('execute')
			->with("UPDATE `$db`.`articles` SET `field1` = 'value1'  WHERE 1 = 1");

		$this->Dbo->expects($this->at(1))->method('execute')
			->with("UPDATE `$db`.`articles` AS `Article` LEFT JOIN `$db`.`users` AS `User` ON " .
				"(`Article`.`user_id` = `User`.`id`)" .
				" SET `Article`.`field1` = 2  WHERE 2=2");

		$this->Dbo->expects($this->at(2))->method('execute')
			->with("UPDATE `$db`.`articles` AS `Article` LEFT JOIN `$db`.`users` AS `User` ON " .
				"(`Article`.`user_id` = `User`.`id`)" .
				" SET `Article`.`field1` = 'value'  WHERE `index` = 'val'");

		$Article = new Article();

		$this->Dbo->update($Article, array('field1'), array('value1'));
		$this->Dbo->update($Article, array('field1'), array('2'), '2=2');
		$this->Dbo->update($Article, array('field1'), array("'value'"), array('index' => 'val'));
	}
All Usage Examples Of DboSource::update
DboSource