From da2988a22b9c0a0dd9fedfa2be0a67c3c1ac3e80 Mon Sep 17 00:00:00 2001 From: "mariano.iglesias" Date: Thu, 28 Jun 2007 17:31:26 +0000 Subject: [PATCH] Improving HABTM saving by getting rid of unneeded loop and escaping field names git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5367 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/model.php | 33 ++++++++++----------------------- 1 file changed, 10 insertions(+), 23 deletions(-) diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index ff3db8df0..47e2fc025 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1097,9 +1097,9 @@ class Model extends Overloadable { foreach ($y as $assoc => $value) { if (isset($this->hasAndBelongsToMany[$assoc])) { $joinTable[$assoc] = $this->hasAndBelongsToMany[$assoc]['joinTable']; - $mainKey[$assoc] = $this->hasAndBelongsToMany[$assoc]['foreignKey']; - $keys[] = $this->hasAndBelongsToMany[$assoc]['foreignKey']; - $keys[] = $this->hasAndBelongsToMany[$assoc]['associationForeignKey']; + $mainKey[$assoc] = $db->name($this->hasAndBelongsToMany[$assoc]['foreignKey']); + $keys[] = $db->name($this->hasAndBelongsToMany[$assoc]['foreignKey']); + $keys[] = $db->name($this->hasAndBelongsToMany[$assoc]['associationForeignKey']); $fields[$assoc] = join(',', $keys); unset($keys); @@ -1123,27 +1123,14 @@ class Model extends Overloadable { } } - if (isset($joinTable)) { - $total = count($joinTable); + if (isset($joinTable) && is_array($newValue)) { + foreach ($newValue as $loopAssoc => $val) { + $table = $db->name($db->fullTableName($joinTable[$loopAssoc])); + $db->query("DELETE FROM {$table} WHERE {$mainKey[$loopAssoc]} = '{$id}'"); - if (is_array($newValue)) { - foreach ($newValue as $loopAssoc => $val) { - $db =& ConnectionManager::getDataSource($this->useDbConfig); - $table = $db->name($db->fullTableName($joinTable[$loopAssoc])); - $db->query("DELETE FROM {$table} WHERE {$mainKey[$loopAssoc]} = '{$id}'"); - - if (!empty($newValue[$loopAssoc])) { - $secondCount = count($newValue[$loopAssoc]); - $insertValues = null; - - for ($x = 0; $x < $secondCount; $x++) { - $insertValues .= $newValue[$loopAssoc][$x]; - if($x < $secondCount - 1) { - $insertValues .= ', '; - } - } - $db->query("INSERT INTO {$table} ({$fields[$loopAssoc]}) VALUES {$insertValues};"); - } + if (!empty($newValue[$loopAssoc])) { + $insertValues = implode(', ', $newValue[$loopAssoc]); + $db->query("INSERT INTO {$table} ({$fields[$loopAssoc]}) VALUES {$insertValues};"); } } }