refactor uuid checks into a method

This commit is contained in:
dogmatic69 2012-12-04 02:29:06 +00:00
parent 00d178fa28
commit 521a759911

View file

@ -1721,11 +1721,7 @@ class Model extends Object implements CakeEventListener {
if (!empty($this->id)) {
$success = (bool)$db->update($this, $fields, $values);
} else {
$fInfo = $this->schema($this->primaryKey);
$isUUID = ($fInfo['length'] == 36 &&
($fInfo['type'] === 'string' || $fInfo['type'] === 'binary')
);
if (empty($this->data[$this->alias][$this->primaryKey]) && $isUUID) {
if (empty($this->data[$this->alias][$this->primaryKey]) && $this->isUUID($this->primaryKey)) {
if (array_key_exists($this->primaryKey, $this->data[$this->alias])) {
$j = array_search($this->primaryKey, $fields);
$values[$j] = String::uuid();
@ -1771,6 +1767,17 @@ class Model extends Object implements CakeEventListener {
return $success;
}
/**
* Check if the passed in field is a UUID field
*
* @param string $field the field to check
* @return array
*/
public function isUUID($field) {
$field = $this->schema($field);
return $field['length'] == 36 && in_array($field['type'], array('string', 'binary'));
}
/**
* Saves model hasAndBelongsToMany data to the database.
*
@ -1785,7 +1792,6 @@ class Model extends Object implements CakeEventListener {
if (isset($this->hasAndBelongsToMany[$assoc])) {
list($join) = $this->joinModel($this->hasAndBelongsToMany[$assoc]['with']);
$keyInfo = $this->{$join}->schema($this->{$join}->primaryKey);
if ($with = $this->hasAndBelongsToMany[$assoc]['with']) {
$withModel = is_array($with) ? key($with) : $with;
list($pluginName, $withModel) = pluginSplit($withModel);
@ -1794,12 +1800,7 @@ class Model extends Object implements CakeEventListener {
$dbMulti = $db;
}
$isUUID = !empty($this->{$join}->primaryKey) && (
$keyInfo['length'] == 36 && (
$keyInfo['type'] === 'string' ||
$keyInfo['type'] === 'binary'
)
);
$isUUID = !empty($this->{$join}->primaryKey) && $this->{$join}->isUUID($this->{$join}->primaryKey);
$newData = $newValues = $newJoins = array();
$primaryAdded = false;