mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Adding conditions-based updating to DboSource
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@3709 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
260c257514
commit
57f27126bd
1 changed files with 19 additions and 4 deletions
|
@ -1135,20 +1135,35 @@ class DboSource extends DataSource {
|
|||
* @param mixed $conditions
|
||||
* @return array
|
||||
*/
|
||||
function update(&$model, $fields = array(), $values = array(), $conditions = null) {
|
||||
function update(&$model, $fields = array(), $values = null, $conditions = null) {
|
||||
$updates = array();
|
||||
$combined = array_combine($fields, $values);
|
||||
if ($values == null) {
|
||||
$combined = $fields;
|
||||
} else {
|
||||
$combined = array_combine($fields, $values);
|
||||
}
|
||||
|
||||
foreach($combined as $field => $value) {
|
||||
if ($value === null) {
|
||||
$updates[] = $this->name($field) . ' = NULL';
|
||||
} else {
|
||||
$updates[] = $this->name($field) . ' = ' . $this->value($value, $model->getColumnType($field));
|
||||
$update = $this->name($field) . ' = ';
|
||||
if ($conditions == null) {
|
||||
$update .= $this->value($value, $model->getColumnType($field));
|
||||
} else {
|
||||
$update .= $value;
|
||||
}
|
||||
$updates[] = $update;
|
||||
}
|
||||
}
|
||||
$sql = 'UPDATE ' . $this->fullTableName($model);
|
||||
$sql .= ' SET ' . join(',', $updates);
|
||||
$sql .= ' WHERE ' . $this->name($model->primaryKey) . ' = ' . $this->value($model->getID(), $model->getColumnType($model->primaryKey));
|
||||
|
||||
if ($conditions == null) {
|
||||
$sql .= ' WHERE ' . $this->name($model->primaryKey) . ' = ' . $this->value($model->getID(), $model->getColumnType($model->primaryKey));
|
||||
} else {
|
||||
$sql .= $this->conditions($conditions);
|
||||
}
|
||||
if (!$this->execute($sql)) {
|
||||
$model->onError();
|
||||
return false;
|
||||
|
|
Loading…
Add table
Reference in a new issue