diff --git a/classes/Model.php b/classes/Model.php index 95a0ab8..b579334 100644 --- a/classes/Model.php +++ b/classes/Model.php @@ -1103,6 +1103,10 @@ class Model extends Object * where you could have a mixed lot that would attempt to * build out a query with both INSERT and UPDATE syntax and * would probably cause a doomsday scenario for our universe. + * @todo Doesn't play nice with ->walk() at all. Ends up stuck in + * an infinite loop and never executes. Could be part of the + * aforementioned doomsday scenario and fortunately PHP isn't + * letting it happen thanks to memory constraints. */ foreach ($this->records as $record) { @@ -1151,17 +1155,11 @@ class Model extends Object $sql .= '; '; } - $sql .= 'UPDATE ' . $this->table . ' SET ' . implode(', ', $update_fields) . ' WHERE '; + $sql .= 'UPDATE ' . $this->table + . ' SET ' . implode(', ', $update_fields) + . ' WHERE ' . $this->columns['id'] . ' = ?'; - if (isset($record[$this->columns['id']])) - { - $sql .= $this->columns['id'] . ' = ?'; - $input_parameters[] = $record[$this->columns['id']]; - } - else - { - throw new Exception('Missing UID field.'); - } + $input_parameters[] = $record[$this->columns['id']]; } // Performs a multiple row INSERT else @@ -1224,7 +1222,7 @@ class Model extends Object return $results; } // Single row INSERT or UPDATE - elseif (count($this->record) > 0) + else { // Determines if it's an UPDATE or INSERT $update = (isset($this->record[$this->columns['id']]) && trim($this->record[$this->columns['id']]) != ''); diff --git a/tests/classes/ModelTest.php b/tests/classes/ModelTest.php index e73e19e..967eaaf 100644 --- a/tests/classes/ModelTest.php +++ b/tests/classes/ModelTest.php @@ -603,20 +603,21 @@ class ModelTest extends PHPUnit_Framework_TestCase $this->assertEquals($count + 5, $model->record['count']); } -# public function testMultipleQueueUpdate() -# { -# $model = new MockModel(['conditions' => ['id <=' => 5]]); -# -# var_dump($model->records); -# -# # while ($model->walk()) -# # { -# # $model->record['field1'] = String::random(); -# # $model->queue(); -# # } -# -# # $model->commit(); -# } + public function testMultipleQueueUpdate() + { + $_SESSION['__pickles']['security']['user_id'] = 1; + $model = new MockModel(); + + for ($i = 3; $i <= 5; $i++) + { + $model->record['id'] = $i; + $model->record['field1'] = String::random(); + $model->record['updated_id'] = 1; + $model->queue(); + } + + $model->commit(); + } } ?>