Skip to content

Commit

Permalink
Fixed Query::set() to apply field types when an array is passed, closes
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Feb 16, 2014
1 parent bc5e137 commit faf67c8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/Database/Query.php
Expand Up @@ -1424,14 +1424,18 @@ public function set($key, $value = null, $types = []) {
if (empty($this->_parts['set'])) {
$this->_parts['set'] = $this->newExpr()->type(',');
}
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->defaultTypes());

if (is_array($key) || $key instanceof ExpressionInterface) {
$types = (array)$value;
$this->_parts['set']->add($key, $types + $this->defaultTypes());
return $this;
}

if (is_string($types) && is_string($key)) {
$types = [$key => $types];
}
$this->_parts['set']->eq($key, $value, $types + $this->defaultTypes());

return $this;
}

Expand Down
29 changes: 29 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -1817,6 +1817,35 @@ public function testUpdateWithExpression() {
$this->assertCount(1, $result);
}

/**
* Test update with array fields and types.
*
* @return void
*/
public function testUpdateArrayFields() {
$query = new Query($this->connection);
$date = new \DateTime;
$query->update('comments')
->set(['comment' => 'mark', 'created' => $date], ['created' => 'date'])
->where(['id' => 1]);
$result = $query->sql();

$this->assertQuotedQuery(
'UPDATE <comments> SET <comment> = :c0 , <created> = :c1',
$result,
true
);

$this->assertQuotedQuery(' WHERE <id> = :c2$', $result, true);
$result = $query->execute();
$this->assertCount(1, $result);

$query = new Query($this->connection);
$result = $query->select('created')->from('comments')->where(['id' => 1])->execute();
$result = $result->fetchAll('assoc')[0]['created'];
$this->assertEquals($date->format('Y-m-d'), $result);
}

/**
* You cannot call values() before insert() it causes all sorts of pain.
*
Expand Down

0 comments on commit faf67c8

Please sign in to comment.