Skip to content

Commit

Permalink
Refactoring Model::__validateWithModels. Should be slightly faster as…
Browse files Browse the repository at this point in the history
… loops are smaller.
  • Loading branch information
markstory committed Dec 16, 2009
1 parent a490e24 commit 4ac2996
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions cake/libs/model/model.php
Expand Up @@ -2513,26 +2513,29 @@ function invalidFields($options = array()) {
*/
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;
}
Expand Down

0 comments on commit 4ac2996

Please sign in to comment.