From 521a7599117382f041117f173d426008cf00c5f0 Mon Sep 17 00:00:00 2001 From: dogmatic69 Date: Tue, 4 Dec 2012 02:29:06 +0000 Subject: [PATCH] refactor uuid checks into a method --- lib/Cake/Model/Model.php | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index ed6876a60..2b55a73c4 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -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;