From 4ac29963a880b5470860291b1dfebcdaed130729 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Tue, 15 Dec 2009 09:25:05 -0500 Subject: [PATCH] Refactoring Model::__validateWithModels. Should be slightly faster as loops are smaller. --- cake/libs/model/model.php | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index 7e771a075..f0e0a6e9c 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -2513,26 +2513,29 @@ class Model extends Overloadable { */ function __validateWithModels($options) { $valid = true; - foreach ($this->data as $assoc => $data) { - if (isset($this->hasAndBelongsToMany[$assoc]) && !empty($this->hasAndBelongsToMany[$assoc]['with'])) { - list($join) = $this->joinModel($this->hasAndBelongsToMany[$assoc]['with']); - $newData = array(); - foreach ((array)$data as $row) { - if (isset($row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) { - $newData[] = $row; - } elseif (isset($row[$join]) && isset($row[$join][$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) { - $newData[] = $row[$join]; - } - } - if (empty($newData)) { - continue; - } - foreach ($newData as $data) { - $data[$this->hasAndBelongsToMany[$assoc]['foreignKey']] = $this->id; - $this->{$join}->create($data); - $valid = ($valid && $this->{$join}->validates($options)); + foreach ($this->hasAndBelongsToMany as $assoc => $association) { + if (empty($association['with']) || !isset($this->data[$assoc])) { + continue; + } + list($join) = $this->joinModel($this->hasAndBelongsToMany[$assoc]['with']); + $data = $this->data[$assoc]; + + $newData = array(); + foreach ((array)$data as $row) { + if (isset($row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) { + $newData[] = $row; + } elseif (isset($row[$join]) && isset($row[$join][$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) { + $newData[] = $row[$join]; } } + if (empty($newData)) { + continue; + } + foreach ($newData as $data) { + $data[$this->hasAndBelongsToMany[$assoc]['foreignKey']] = $this->id; + $this->{$join}->create($data); + $valid = ($valid && $this->{$join}->validates($options)); + } } return $valid; }