ok, I found it!  the __saveMulti -method in the model class shuffels
the $key=>$value association of the given data if there are some keys
of the dependent model not given!
This is because __saveMulti  expects to be given all dependent-keys for
that model. But if we have a 0:n-relationship it might be that some
dependent keys are not set. Therefore the arrays in __saveMulti must be
reference by associative keys.

This is my new version of save_multi: (any comments welcome)

/**
 * Saves model hasAndBelongsToMany data to the database.
 *
 * @param array $joined Data to save.
 * @param string $id
 * @return
 * @access private
 */
        function __saveMulti($joined, $id) {
                $db =& ConnectionManager::getDataSource($this->useDbConfig);
         foreach($joined as $x => $y) {
                        foreach($y as $assoc => $value) {

                                $joinTable[$assoc] =
$this->hasAndBelongsToMany[$assoc]['joinTable'];
                                $mainKey[$assoc] =
$this->hasAndBelongsToMany[$assoc]['foreignKey'];
                                $keys[]    = 
$this->hasAndBelongsToMany[$assoc]['foreignKey'];
                                $keys[]    =
$this->hasAndBelongsToMany[$assoc]['associationForeignKey'];
                                $fields[$assoc]  = join(',', $keys);
                                unset($keys);

                                foreach($value as $update) {
                                        if (!empty($update)) {
                                                $values[]  = $db->value($id,
$this->getColumnType($this->primaryKey));
                                                $values[]  = 
$db->value($update);
                                                $values    = join(',', $values);
                                                $newValues[] = "({$values})";
                                                unset ($values);
                                        }
                                }

                                if (!empty($newValues)) {
                                        $newValue[$assoc] = $newValues;
                                        unset($newValues);
                                } else {
                                        $newValue[$assoc]=array();
                                }
                        }
                }

                $total = count($joinTable);

        if(is_array($newValue)){
                        foreach ($newValue as $loop_assoc=>$val) {
                                $db =& 
ConnectionManager::getDataSource($this->useDbConfig);
                                $table = 
$db->name($db->fullTableName($joinTable[$loop_assoc]));
                                $db->execute("DELETE FROM {$table} WHERE 
{$mainKey[$loop_assoc]} =
'{$id}'");

                                if (!empty($newValue[$loop_assoc])) {
                                        $secondCount = 
count($newValue[$loop_assoc]);

                                        for($x = 0; $x < $secondCount; $x++) {
                                                $db->execute("INSERT INTO 
{$table} ({$fields[$loop_assoc]})
VALUES {$newValue[$loop_assoc][$x]}");
                                        }
                                }
                        }
        }

        }


greets
felle


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Cake 
PHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/cake-php
-~----------~----~----~----~------~----~------~--~---

Reply via email to