Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Prevent sql error for uuids if id is specified as null

if the primary key is present in the data to be saved as null - prevent
passing the same key (id) twice and therefore triggering an sql error.

Signed-off-by: Mark Story <mark@mark-story.com>
  • Loading branch information...
commit bc990f41e3d29db70cf5c181a11668c8e63f4bdf 1 parent 01a5738
@AD7six AD7six authored markstory committed
View
7 cake/libs/model/model.php
@@ -1273,7 +1273,12 @@ function save($data = null, $validate = true, $fieldList = array()) {
($fInfo['type'] === 'string' || $fInfo['type'] === 'binary')
);
if (empty($this->data[$this->alias][$this->primaryKey]) && $isUUID) {
- list($fields[], $values[]) = array($this->primaryKey, String::uuid());
+ if (array_key_exists($this->primaryKey, $this->data[$this->alias])) {
+ $j = array_search($this->primaryKey, $fields);
+ $values[$j] = String::uuid();
+ } else {
+ list($fields[], $values[]) = array($this->primaryKey, String::uuid());
+ }
}
break;
}
View
23 cake/tests/cases/libs/model/model_write.test.php
@@ -162,6 +162,29 @@ function testAutoSaveUuid() {
$this->assertEqual(strlen($result['Uuid']['id']), 36);
}
/**
+ * Ensure that if the id key is null but present the save doesn't fail (with an
+ * x sql error: "Column id specified twice")
+ *
+ * @return void
+ * @access public
+ */
+ function testSaveUuidNull() {
+ // SQLite does not support non-integer primary keys
+ $this->skipIf($this->db->config['driver'] == 'sqlite');
+
+ $this->loadFixtures('Uuid');
+ $TestModel =& new Uuid();
+
+ $TestModel->save(array('title' => 'Test record', 'id' => null));
+ $result = $TestModel->findByTitle('Test record');
+ $this->assertEqual(
+ array_keys($result['Uuid']),
+ array('id', 'title', 'count', 'created', 'updated')
+ );
+ $this->assertEqual(strlen($result['Uuid']['id']), 36);
+ }
+
+/**
* testZeroDefaultFieldValue method
*
* @access public
Please sign in to comment.
Something went wrong with that request. Please try again.