mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
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>
This commit is contained in:
parent
4861da66ed
commit
0c951b7248
2 changed files with 30 additions and 2 deletions
|
@ -1334,7 +1334,12 @@ class Model extends Overloadable {
|
|||
($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;
|
||||
}
|
||||
|
@ -2667,7 +2672,7 @@ class Model extends Overloadable {
|
|||
}
|
||||
|
||||
/**
|
||||
* Escapes the field name and prepends the model name. Escaping is done according to the
|
||||
* Escapes the field name and prepends the model name. Escaping is done according to the
|
||||
* current database driver's rules.
|
||||
*
|
||||
* @param string $field Field to escape (e.g: id)
|
||||
|
|
|
@ -162,6 +162,29 @@ class ModelWriteTest extends BaseModelTest {
|
|||
$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
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue