mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
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:
parent
2e54eb1706
commit
da2988a22b
1 changed files with 10 additions and 23 deletions
|
@ -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};");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue