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:
nate 2006-10-18 15:38:37 +00:00
parent 260c257514
commit 57f27126bd

View file

@ -1135,20 +1135,35 @@ class DboSource extends DataSource {
* @param mixed $conditions * @param mixed $conditions
* @return array * @return array
*/ */
function update(&$model, $fields = array(), $values = array(), $conditions = null) { function update(&$model, $fields = array(), $values = null, $conditions = null) {
$updates = array(); $updates = array();
if ($values == null) {
$combined = $fields;
} else {
$combined = array_combine($fields, $values); $combined = array_combine($fields, $values);
}
foreach($combined as $field => $value) { foreach($combined as $field => $value) {
if ($value === null) { if ($value === null) {
$updates[] = $this->name($field) . ' = NULL'; $updates[] = $this->name($field) . ' = NULL';
} else { } 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 = 'UPDATE ' . $this->fullTableName($model);
$sql .= ' SET ' . join(',', $updates); $sql .= ' SET ' . join(',', $updates);
if ($conditions == null) {
$sql .= ' WHERE ' . $this->name($model->primaryKey) . ' = ' . $this->value($model->getID(), $model->getColumnType($model->primaryKey)); $sql .= ' WHERE ' . $this->name($model->primaryKey) . ' = ' . $this->value($model->getID(), $model->getColumnType($model->primaryKey));
} else {
$sql .= $this->conditions($conditions);
}
if (!$this->execute($sql)) { if (!$this->execute($sql)) {
$model->onError(); $model->onError();
return false; return false;