Browse Source

Changing how insertMulti handles boolean values.

Binding values individually allows boolean to work correctly.
mark_story 14 years ago
parent
commit
0b0d180aad

+ 26 - 4
lib/Cake/Model/Datasource/DboSource.php

@@ -2734,9 +2734,11 @@ class DboSource extends DataSource {
 /**
  * Inserts multiple values into a table
  *
- * @param string $table
- * @param string $fields
- * @param array $values
+ * @param string $table The table being inserted into.
+ * @param array $fields The array of field/column names being inserted.
+ * @param array $values The array of values to insert.  The values should
+ *   be an array of rows.  Each row should have values keyed by the column name.
+ *   Each row must have the values in the same order as $fields.
  * @return boolean
  */
 	public function insertMulti($table, $fields, $values) {
@@ -2744,12 +2746,32 @@ class DboSource extends DataSource {
 		$holder = implode(',', array_fill(0, count($fields), '?'));
 		$fields = implode(', ', array_map(array(&$this, 'name'), $fields));
 
+		$pdoMap = array(
+			'integer' => PDO::PARAM_INT,
+			'float' => PDO::PARAM_STR,
+			'boolean' => PDO::PARAM_BOOL,
+			'string' => PDO::PARAM_STR,
+			'text' => PDO::PARAM_STR
+		);
+		$columnMap = array();
+
 		$count = count($values);
 		$sql = "INSERT INTO {$table} ({$fields}) VALUES ({$holder})";
 		$statement = $this->_connection->prepare($sql);
 		$this->begin();
+
+		foreach ($values[0] as $key => $val) {
+			$type = $this->introspectType($val);
+			$columnMap[$key] = $pdoMap[$type];
+		}
+
 		for ($x = 0; $x < $count; $x++) {
-			$statement->execute($values[$x]);
+			$i = 1;
+			foreach ($values[$x] as $key => $val) {
+				$statement->bindValue($i, $val, $columnMap[$key]);
+				$i += 1;
+			}
+			$statement->execute();
 			$statement->closeCursor();
 		}
 		return $this->commit();

+ 1 - 1
lib/Cake/Test/Fixture/CounterCachePostFixture.php

@@ -34,7 +34,7 @@ class CounterCachePostFixture extends CakeTestFixture {
 	);
 
     public $records = array(
-		array('id' => 1, 'title' => 'Rock and Roll',  'user_id' => 66, 'published' => 0),
+		array('id' => 1, 'title' => 'Rock and Roll',  'user_id' => 66, 'published' => false),
 		array('id' => 2, 'title' => 'Music',   'user_id' => 66, 'published' => true),
 		array('id' => 3, 'title' => 'Food',   'user_id' => 301, 'published' => true),
     );