Permalink
Browse files

refactor uuid checks into a method

  • Loading branch information...
1 parent 00d178f commit 521a7599117382f041117f173d426008cf00c5f0 @dogmatic69 dogmatic69 committed Dec 4, 2012
Showing with 13 additions and 12 deletions.
  1. +13 −12 lib/Cake/Model/Model.php
View
25 lib/Cake/Model/Model.php
@@ -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();
@@ -1772,6 +1768,17 @@ public function save($data = null, $validate = true, $fieldList = array()) {
}
/**
+ * 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.
*
* @param array $joined Data to save
@@ -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);
@@ -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;

0 comments on commit 521a759

Please sign in to comment.