From 0b0d180aada47e03e90985ef6d9156f0398c47ee Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 9 Nov 2011 22:31:06 -0500 Subject: [PATCH] Changing how insertMulti handles boolean values. Binding values individually allows boolean to work correctly. --- lib/Cake/Model/Datasource/DboSource.php | 30 ++++++++++++++++--- .../Test/Fixture/CounterCachePostFixture.php | 2 +- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Model/Datasource/DboSource.php b/lib/Cake/Model/Datasource/DboSource.php index 0c655bbc6..967f4bade 100644 --- a/lib/Cake/Model/Datasource/DboSource.php +++ b/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(); diff --git a/lib/Cake/Test/Fixture/CounterCachePostFixture.php b/lib/Cake/Test/Fixture/CounterCachePostFixture.php index 9a5767dbb..dd7b92f1c 100644 --- a/lib/Cake/Test/Fixture/CounterCachePostFixture.php +++ b/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), );