Browse Source

Fix warning when attempting to insert with 0 columns.

mark_story 12 years ago
parent
commit
d95b678467

+ 0 - 2
src/Database/Expression/ValuesExpression.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * PHP Version 5.4
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *

+ 4 - 0
src/Database/Query.php

@@ -1284,8 +1284,12 @@ class Query implements ExpressionInterface, IteratorAggregate {
  * @param array $columns The columns to insert into.
  * @param array $types A map between columns & their datatypes.
  * @return Query
+ * @throws RuntimeException When there are 0 columns.
  */
 	public function insert($columns, $types = []) {
+		if (empty($columns)) {
+			throw new \RuntimeException('At least 1 column is required to perform an insert.');
+		}
 		$this->_dirty();
 		$this->_type = 'insert';
 		$this->_parts['insert'][1] = $columns;

+ 12 - 2
tests/TestCase/Database/QueryTest.php

@@ -1,7 +1,5 @@
 <?php
 /**
- * PHP Version 5.4
- *
  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  *
@@ -1696,6 +1694,18 @@ class QueryTest extends TestCase {
 	}
 
 /**
+ * Inserting nothing should not generate an error.
+ *
+ * @expectedException RuntimeException
+ * @expectedExceptionMessage At least 1 column is required to perform an insert.
+ * @return void
+ */
+	public function testInsertNothing() {
+		$query = new Query($this->connection);
+		$query->insert([]);
+	}
+
+/**
  * Test inserting a single row.
  *
  * @return void