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. |
Результат | boolean | Success |
/** * 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')); }
/** * Generates and executes an SQL UPDATE statement for given model, fields, and values. * * @param Model $model * @param array $fields * @param array $values * @param mixed $conditions * @return array */ function update(&$model, $fields = array(), $values = null, $conditions = null) { if (!$this->_useAlias) { return parent::update($model, $fields, $values, $conditions); } if ($values == null) { $combined = $fields; } else { $combined = array_combine($fields, $values); } $alias = $joins = false; $fields = $this->_prepareUpdateFields($model, $combined, empty($conditions), !empty($conditions)); $fields = join(', ', $fields); $table = $this->fullTableName($model); if (!empty($conditions)) { $alias = $this->name($model->alias); if ($model->name == $model->alias) { $joins = implode(' ', $this->_getJoins($model)); } } $conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), true, true, $model); if ($conditions === false) { return false; } if (!$this->execute($this->renderStatement('update', compact('table', 'alias', 'joins', 'fields', 'conditions')))) { $model->onError(); return false; } return true; }
/** * Generates and executes an SQL UPDATE statement for given model, fields, and values. * * @param Model $model The model instance to update. * @param array $fields The fields to update. * @param array $values The values to set columns to. * @param mixed $conditions array of conditions to use. * @return array */ public function update(Model $model, $fields = array(), $values = null, $conditions = null) { if (empty($values) && !empty($fields)) { foreach ($fields as $field => $value) { if (strpos($field, $model->alias . '.') !== false) { unset($fields[$field]); $field = str_replace($model->alias . '.', "", $field); $field = str_replace($model->alias . '.', "", $field); $fields[$field] = $value; } } } return parent::update($model, $fields, $values, $conditions); }
/** * testUpdateAllSyntax method * * @return void */ public function testUpdateAllSyntax() { $fields = array('SqlserverTestModel.client_id' => '[SqlserverTestModel].[client_id] + 1'); $conditions = array('SqlserverTestModel.updated <' => date('2009-01-01 00:00:00')); $this->db->update($this->model, $fields, null, $conditions); $result = $this->db->getLastQuery(); $this->assertNotRegExp('/SqlserverTestModel/', $result); $this->assertRegExp('/^UPDATE \\[sqlserver_test_models\\]/', $result); $this->assertRegExp('/SET \\[client_id\\] = \\[client_id\\] \\+ 1/', $result); }
/** * Generates and executes an SQL UPDATE statement for given model, fields, and values. * Removes Identity (primary key) column from update data before returning to parent. * * @param Model $model * @param array $fields * @param array $values * @param mixed $conditions * @return array */ function update(&$model, $fields = array(), $values = null, $conditions = null) { if (!empty($values)) { $fields = array_combine($fields, $values); } if (isset($fields[$model->primaryKey])) { unset($fields[$model->primaryKey]); } if (empty($fields)) { return true; } return parent::update($model, array_keys($fields), array_values($fields), $conditions); }
/** * Removes Identity (primary key) column from update data before returning to parent * * @param Model $model * @param array $fields * @param array $values * @return array */ function update(&$model, $fields = array(), $values = array()) { foreach ($fields as $i => $field) { if ($field == $model->primaryKey) { unset($fields[$i]); unset($values[$i]); break; } } return parent::update($model, $fields, $values); }
/** * testUpdateAllSyntax method * * @return void * @access public */ function testUpdateAllSyntax() { $fields = array('MssqlTestModel.client_id' => '[MssqlTestModel].[client_id] + 1'); $conditions = array('MssqlTestModel.updated <' => date('2009-01-01 00:00:00')); $this->db->update($this->model, $fields, null, $conditions); $result = $this->db->getLastQuery(); $this->assertNoPattern('/MssqlTestModel/', $result); $this->assertPattern('/^UPDATE \\[mssql_test_models\\]/', $result); $this->assertPattern('/SET \\[client_id\\] = \\[client_id\\] \\+ 1/', $result); }
/** * Generates and executes an SQL UPDATE statement for given model, fields, and values. * * @param Model $model The model to update. * @param array $fields The fields to update. * @param array $values The values to set. * @param mixed $conditions The conditions to use. * * @return array */ public function update(Model $model, $fields = array(), $values = NULL, $conditions = NULL) { if (!$this->_useAlias) { return parent::update($model, $fields, $values, $conditions); } if (!$values) { $combined = $fields; } else { $combined = array_combine($fields, $values); } $alias = $joins = FALSE; $fields = $this->_prepareUpdateFields($model, $combined, empty($conditions), !empty($conditions)); $fields = implode(', ', $fields); $table = $this->fullTableName($model); if (!empty($conditions)) { $alias = $this->name($model->alias); if ($model->name === $model->alias) { $joins = implode(' ', $this->_getJoins($model)); } } $conditions = $this->conditions($this->defaultConditions($model, $conditions, $alias), TRUE, TRUE, $model); if ($conditions === FALSE) { return FALSE; } if (!$this->execute($this->renderStatement('update', compact('table', 'alias', 'joins', 'fields', 'conditions')))) { $model->onError(); return FALSE; } return TRUE; }
/** * Generates and executes an SQL UPDATE statement for given model, fields, and values. * Removes Identity (primary key) column from update data before returning to parent. * * @param Model $model The model to update. * @param array $fields The fields to set. * @param array $values The values to set. * @param mixed $conditions The conditions to use. * * @return array */ public function update(Model $model, $fields = array(), $values = NULL, $conditions = NULL) { if (!empty($values)) { $fields = array_combine($fields, $values); } if (isset($fields[$model->primaryKey])) { unset($fields[$model->primaryKey]); } if (empty($fields)) { return TRUE; } return parent::update($model, array_keys($fields), array_values($fields), $conditions); }