Skip to content

Commit

Permalink
refactor uuid checks into a method
Browse files Browse the repository at this point in the history
  • Loading branch information
dogmatic69 committed Dec 7, 2012
1 parent 00d178f commit 521a759
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions lib/Cake/Model/Model.php
Expand Up @@ -1721,11 +1721,7 @@ public function save($data = null, $validate = true, $fieldList = array()) {
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();
Expand Down Expand Up @@ -1771,6 +1767,17 @@ public function save($data = null, $validate = true, $fieldList = array()) {
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.
*
Expand All @@ -1785,7 +1792,6 @@ protected function _saveMulti($joined, $id, $db) {
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);
Expand All @@ -1794,12 +1800,7 @@ protected function _saveMulti($joined, $id, $db) {
$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;
Expand Down

0 comments on commit 521a759

Please sign in to comment.