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
This commit is contained in:
mariano.iglesias 2007-06-28 17:31:26 +00:00
parent 2e54eb1706
commit da2988a22b

View file

@ -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,31 +1123,18 @@ class Model extends Overloadable {
}
}
if (isset($joinTable)) {
$total = count($joinTable);
if (is_array($newValue)) {
if (isset($joinTable) && 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 .= ', ';
}
}
$insertValues = implode(', ', $newValue[$loopAssoc]);
$db->query("INSERT INTO {$table} ({$fields[$loopAssoc]}) VALUES {$insertValues};");
}
}
}
}
}
/**
* Allows model records to be updated based on a set of conditions
*