diff --git a/Cake/Database/Connection.php b/Cake/Database/Connection.php index e3c4584af9d..40226472903 100644 --- a/Cake/Database/Connection.php +++ b/Cake/Database/Connection.php @@ -269,7 +269,8 @@ public function schemaCollection() { */ public function insert($table, array $data, array $types = []) { $columns = array_keys($data); - return $this->newQuery()->insert($table, $columns, $types) + return $this->newQuery()->insert($columns, $types) + ->into($table) ->values($data) ->execute(); } diff --git a/Cake/Database/IdentifierQuoter.php b/Cake/Database/IdentifierQuoter.php index 7e4266d355d..bc7157a26e1 100644 --- a/Cake/Database/IdentifierQuoter.php +++ b/Cake/Database/IdentifierQuoter.php @@ -164,7 +164,7 @@ protected function _quoteInsert($query) { $column = $this->_driver->quoteIdentifier($column); } } - $query->insert($table, $columns); + $query->insert($columns)->into($table); } /** diff --git a/Cake/Database/Query.php b/Cake/Database/Query.php index 2750c6ec35a..034d2ed8fad 100644 --- a/Cake/Database/Query.php +++ b/Cake/Database/Query.php @@ -1292,15 +1292,14 @@ protected function _stringifyExpressions(array $expressions, ValueBinder $genera * Note calling this method will reset any data previously set * with Query::values() * - * @param string $table The table name to insert into. * @param array $columns The columns to insert into. * @param array $types A map between columns & their datatypes. * @return Query */ - public function insert($table, $columns, $types = []) { + public function insert($columns, $types = []) { $this->_dirty(); $this->_type = 'insert'; - $this->_parts['insert'] = [$table, $columns]; + $this->_parts['insert'][1] = $columns; if (!$this->_parts['values']) { $this->_parts['values'] = new ValuesExpression($columns, $types + $this->defaultTypes()); @@ -1309,6 +1308,19 @@ public function insert($table, $columns, $types = []) { return $this; } +/** + * Set the table name for insert queries. + * + * @param string $table The table name to insert into. + * @return Query + */ + public function into($table) { + $this->_dirty(); + $this->_type = 'insert'; + $this->_parts['insert'][0] = $table; + return $this; + } + /** * Set the values for an insert query. * diff --git a/Cake/ORM/Query.php b/Cake/ORM/Query.php index 06c944190d6..8c1bc48643b 100644 --- a/Cake/ORM/Query.php +++ b/Cake/ORM/Query.php @@ -986,12 +986,12 @@ public function delete($table = null) { * * @param array $columns The columns to insert into. * @param array $types A map between columns & their datatypes. - * @param array $unused An unused parameter from the parent class' interface * @return Query */ - public function insert($columns, $types = [], $unused = []) { + public function insert($columns, $types = []) { $table = $this->repository()->table(); - return parent::insert($table, $columns, $types); + $this->into($table); + return parent::insert($columns, $types); } } diff --git a/Cake/Test/TestCase/Database/Driver/PostgresTest.php b/Cake/Test/TestCase/Database/Driver/PostgresTest.php index 306ba243fbf..665be7d0b5e 100644 --- a/Cake/Test/TestCase/Database/Driver/PostgresTest.php +++ b/Cake/Test/TestCase/Database/Driver/PostgresTest.php @@ -1,7 +1,5 @@ false]] ); $query = new \Cake\Database\Query($connection); - $query->insert('articles', ['id', 'title'])->values([1, 'foo']); + $query->insert(['id', 'title']) + ->into('articles') + ->values([1, 'foo']); $translator = $driver->queryTranslator('insert'); $query = $translator($query); $this->assertEquals('RETURNING *', $query->clause('epilog')); $query = new \Cake\Database\Query($connection); - $query->insert('articles', ['id', 'title'])->values([1, 'foo'])->epilog('FOO'); + $query->insert(['id', 'title']) + ->into('articles') + ->values([1, 'foo']) + ->epilog('FOO'); $query = $translator($query); $this->assertEquals('FOO', $query->clause('epilog')); } diff --git a/Cake/Test/TestCase/Database/Log/QueryLoggerTest.php b/Cake/Test/TestCase/Database/Log/QueryLoggerTest.php index f2e5151f12a..7d49ede248f 100644 --- a/Cake/Test/TestCase/Database/Log/QueryLoggerTest.php +++ b/Cake/Test/TestCase/Database/Log/QueryLoggerTest.php @@ -91,10 +91,13 @@ public function testLogFunction() { $query = new LoggedQuery; $query->query = 'SELECT a FROM b where a = ? AND b = ? AND c = ?'; $query->params = ['string', '3', null]; + $engine = $this->getMock('\Cake\Log\Engine\BaseLog', ['write'], ['scopes' => ['queriesLog']]); Log::engine('queryLoggerTest', $engine); + $engine2 = $this->getMock('\Cake\Log\Engine\BaseLog', ['write'], ['scopes' => ['foo']]); - Log::engine('queryLoggerTest2', $engine); + Log::engine('queryLoggerTest2', $engine2); + $engine2->expects($this->never())->method('write'); $logger->log($query); } diff --git a/Cake/Test/TestCase/Database/QueryTest.php b/Cake/Test/TestCase/Database/QueryTest.php index a44a71a583e..860224e12e0 100644 --- a/Cake/Test/TestCase/Database/QueryTest.php +++ b/Cake/Test/TestCase/Database/QueryTest.php @@ -1702,7 +1702,8 @@ public function testInsertValuesBeforeInsertFailure() { */ public function testInsertSimple() { $query = new Query($this->connection); - $query->insert('articles', ['title', 'body']) + $query->insert(['title', 'body']) + ->into('articles') ->values([ 'title' => 'mark', 'body' => 'test insert' @@ -1738,7 +1739,8 @@ public function testInsertSimple() { */ public function testInsertSparseRow() { $query = new Query($this->connection); - $query->insert('articles', ['title', 'body']) + $query->insert(['title', 'body']) + ->into('articles') ->values([ 'title' => 'mark', ]); @@ -1772,7 +1774,8 @@ public function testInsertSparseRow() { */ public function testInsertMultipleRowsSparse() { $query = new Query($this->connection); - $query->insert('articles', ['title', 'body']) + $query->insert(['title', 'body']) + ->into('articles') ->values([ 'body' => 'test insert' ]) @@ -1814,10 +1817,10 @@ public function testInsertFromSelect() { $query = new Query($this->connection); $query->insert( - 'articles', ['title', 'body', 'author_id'], ['title' => 'string', 'body' => 'string', 'author_id' => 'integer'] ) + ->into('articles') ->values($select); $result = $query->sql(); @@ -1853,7 +1856,8 @@ public function testInsertFromSelect() { */ public function testInsertFailureMixingTypesArrayFirst() { $query = new Query($this->connection); - $query->insert('articles', ['name']) + $query->insert(['name']) + ->into('articles') ->values(['name' => 'mark']) ->values(new Query($this->connection)); } @@ -1865,7 +1869,8 @@ public function testInsertFailureMixingTypesArrayFirst() { */ public function testInsertFailureMixingTypesQueryFirst() { $query = new Query($this->connection); - $query->insert('articles', ['name']) + $query->insert(['name']) + ->into('articles') ->values(new Query($this->connection)) ->values(['name' => 'mark']); } @@ -2015,7 +2020,8 @@ public function testAppendSelect() { public function testAppendInsert() { $query = new Query($this->connection); $sql = $query - ->insert('articles', ['id', 'title']) + ->insert(['id', 'title']) + ->into('articles') ->values([1, 'a title']) ->epilog('RETURNING id') ->sql(); @@ -2197,13 +2203,15 @@ public function testQuotingExpressions() { public function testQuotingInsert() { $this->connection->driver()->autoQuoting(true); $query = new Query($this->connection); - $sql = $query->insert('foo', ['bar', 'baz']) + $sql = $query->insert(['bar', 'baz']) + ->into('foo') ->where(['something' => 'value']) ->sql(); $this->assertQuotedQuery('INSERT INTO \(, \)', $sql); $query = new Query($this->connection); - $sql = $query->insert('foo', [$query->newExpr()->add('bar')]) + $sql = $query->insert([$query->newExpr()->add('bar')]) + ->into('foo') ->where(['something' => 'value']) ->sql(); $this->assertQuotedQuery('INSERT INTO \(\(bar\)\)', $sql); diff --git a/Cake/Test/TestCase/TestSuite/TestFixtureTest.php b/Cake/Test/TestCase/TestSuite/TestFixtureTest.php index 546caece068..fe40d8dc92f 100644 --- a/Cake/Test/TestCase/TestSuite/TestFixtureTest.php +++ b/Cake/Test/TestCase/TestSuite/TestFixtureTest.php @@ -234,22 +234,28 @@ public function testInsert() { $query->expects($this->once()) ->method('insert') - ->with('articles', ['name', 'created'], ['string', 'datetime']) + ->with(['name', 'created'], ['string', 'datetime']) ->will($this->returnSelf()); + + $query->expects($this->once()) + ->method('into') + ->with('articles') + ->will($this->returnSelf()); + $expected = [ ['name' => 'Gandalf', 'created' => '2009-04-28 19:20:00'], ['name' => 'Captain Picard', 'created' => '2009-04-28 19:20:00'], ['name' => 'Chewbacca', 'created' => '2009-04-28 19:20:00'] ]; - $query->expects($this->at(1)) + $query->expects($this->at(2)) ->method('values') ->with($expected[0]) ->will($this->returnSelf()); - $query->expects($this->at(2)) + $query->expects($this->at(3)) ->method('values') ->with($expected[1]) ->will($this->returnSelf()); - $query->expects($this->at(3)) + $query->expects($this->at(4)) ->method('values') ->with($expected[2]) ->will($this->returnSelf()); @@ -279,7 +285,12 @@ public function testInsertStrings() { $query->expects($this->once()) ->method('insert') - ->with('strings', ['name', 'email', 'age'], ['string', 'string', 'integer']) + ->with(['name', 'email', 'age'], ['string', 'string', 'integer']) + ->will($this->returnSelf()); + + $query->expects($this->once()) + ->method('into') + ->with('strings') ->will($this->returnSelf()); $expected = [ @@ -287,15 +298,15 @@ public function testInsertStrings() { ['name' => 'John Doe', 'email' => 'john.doe@email.com', 'age' => 20], ['name' => 'Jane Doe', 'email' => 'jane.doe@email.com', 'age' => 30], ]; - $query->expects($this->at(1)) + $query->expects($this->at(2)) ->method('values') ->with($expected[0]) ->will($this->returnSelf()); - $query->expects($this->at(2)) + $query->expects($this->at(3)) ->method('values') ->with($expected[1]) ->will($this->returnSelf()); - $query->expects($this->at(3)) + $query->expects($this->at(4)) ->method('values') ->with($expected[2]) ->will($this->returnSelf()); diff --git a/Cake/TestSuite/Fixture/TestFixture.php b/Cake/TestSuite/Fixture/TestFixture.php index d28436a1bae..2bd8ca30954 100644 --- a/Cake/TestSuite/Fixture/TestFixture.php +++ b/Cake/TestSuite/Fixture/TestFixture.php @@ -276,7 +276,8 @@ public function insert(Connection $db) { if (isset($this->records) && !empty($this->records)) { list($fields, $values, $types) = $this->_getRecords(); $query = $db->newQuery() - ->insert($this->table, $fields, $types); + ->insert($fields, $types) + ->into($this->table); foreach ($values as $row) { $query->values($row); diff --git a/Cake/Validation/Validator.php b/Cake/Validation/Validator.php index 3d42972f323..93f058f8dc2 100644 --- a/Cake/Validation/Validator.php +++ b/Cake/Validation/Validator.php @@ -386,7 +386,8 @@ protected function _fieldIsEmpty($data) { */ protected function _processRules(ValidationSet $rules, $value, $newRecord) { $errors = []; - $this->provider('default'); // Loading default provider in case there is none + // Loading default provider in case there is none + $this->provider('default'); foreach ($rules as $name => $rule) { $result = $rule->process($value, $this->_providers, $newRecord); if ($result === true) {