Skip to content

Commit

Permalink
Squashed query update type check
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Chater committed Jan 18, 2017
1 parent e808d23 commit 0fdae8e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Database/Query.php
Expand Up @@ -19,6 +19,7 @@
use Cake\Database\Expression\QueryExpression;
use Cake\Database\Expression\ValuesExpression;
use Cake\Database\Statement\CallbackStatement;
use InvalidArgumentException;
use IteratorAggregate;
use RuntimeException;

Expand Down Expand Up @@ -1412,11 +1413,17 @@ public function values($data)
*
* Can be combined with set() and where() methods to create update queries.
*
* @param string $table The table you want to update.
* @param string|\Cake\Database\ExpressionInterface $table The table you want to update.
* @return $this
*/
public function update($table)
{
if (!is_string($table) && !($table instanceof ExpressionInterface)) {
$text = 'Table must be of type string or "%s", got "%s"';
$message = sprintf($text, ExpressionInterface::class, gettype($table));
throw new InvalidArgumentException($message);
}

$this->_dirty();
$this->_type = 'update';
$this->_parts['update'][0] = $table;
Expand Down
14 changes: 14 additions & 0 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -2731,6 +2731,20 @@ public function testUpdateSimple()
$result->closeCursor();
}

/**
* Test update with type checking
* by passing an array as table arg
*
* @expectedException \InvalidArgumentException
*
* @return void
*/
public function testUpdateArgTypeChecking()
{
$query = new Query($this->connection);
$query->update(['Articles']);
}

/**
* Test update with multiple fields.
*
Expand Down

0 comments on commit 0fdae8e

Please sign in to comment.