Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add type support into set().
Update argument order and make the set() easy to use. Also
fix multiple tables being appended to update when called multiple times.
Now it retains the last value.
  • Loading branch information
markstory committed Mar 3, 2013
1 parent 7384a49 commit 674c973
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
23 changes: 14 additions & 9 deletions lib/Cake/Model/Datasource/Database/Query.php
Expand Up @@ -1126,7 +1126,7 @@ public function insert() {
public function update($table) {
$this->_dirty = true;
$this->_type = 'update';
$this->_parts['update'][] = $table;
$this->_parts['update'][0] = $table;
return $this;
}

Expand All @@ -1135,19 +1135,24 @@ public function update($table) {
*
* @param string|array|QueryExpression $key The column name or array of keys
* + values to set. This can also be a QueryExpression containing a SQL fragment.
* @param mixed $value The value to update $key to. Can be null if $key is an
* array or QueryExpression
* @param mixed $value The value to update $key to. Can be null if $key is an
* array or QueryExpression. When $key is an array, this parameter will be
* used as $types instead.
* @param array $types The column types to treat data as.
* @return Query
*/
public function set($key, $value = null) {
public function set($key, $value = null, $types = []) {
if (empty($this->_parts['set'])) {
$this->_parts['set'] = new QueryExpression([], [], ',');
$this->_parts['set'] = $this->newExpr()->type(',');
}
$set = $key;
if (!is_array($key) && !($key instanceof QueryExpression)) {
$set = [$key => $value];
if (is_array($key) || $key instanceof QueryExpression) {
$this->_parts['set']->add($key, (array)$value);
} else {
if (is_string($types)) {
$types = [$key => $types];
}
$this->_parts['set']->add([$key => $value], $types);
}
$this->_parts['set']->add($set, []);
return $this;
}

Expand Down
11 changes: 8 additions & 3 deletions lib/Cake/Test/TestCase/Model/Datasource/Database/QueryTest.php
Expand Up @@ -1659,8 +1659,8 @@ public function testUpdateMultipleFields() {
$this->_insertTwoRecords();
$query = new Query($this->connection);
$query->update('articles')
->set('title', 'mark')
->set('body', 'some text')
->set('title', 'mark', 'string')
->set('body', 'some text', 'string')
->where(['id' => 1]);
$result = $query->sql(false);

Expand All @@ -1686,7 +1686,7 @@ public function testUpdateMultipleFieldsArray() {
->set([
'title' => 'mark',
'body' => 'some text'
])
], ['title' => 'string', 'body' => 'string'])
->where(['id' => 1]);
$result = $query->sql(false);

Expand All @@ -1700,6 +1700,11 @@ public function testUpdateMultipleFieldsArray() {
$this->assertCount(1, $result);
}

/**
* Test updates with an expression.
*
* @return void
*/
public function testUpdateWithExpression() {
$this->_insertTwoRecords();
$query = new Query($this->connection);
Expand Down

0 comments on commit 674c973

Please sign in to comment.