01.28 php saved
hiromi2424
Tags add more
habtm and model  
Note
changed function __saveMulti() of Model fot ticket
  1.  
  2. /**
  3. * Saves model hasAndBelongsToMany data to the database.
  4. *
  5. * @param array $joined Data to save
  6. * @param mixed $id ID of record in this model
  7. * @access private
  8. */
  9.     function __saveMulti($joined, $id, &$db) {
  10.         foreach ($joined as $assoc => $data) {
  11.  
  12.             if (isset($this->hasAndBelongsToMany[$assoc])) {
  13.                 list($join) = $this->joinModel($this->hasAndBelongsToMany[$assoc]['with']);
  14.  
  15.                 $isUUID = !empty($this->{$join}->primaryKey) && (
  16.                         $this->{$join}->_schema[$this->{$join}->primaryKey]['length'] == 36 && (
  17.                         $this->{$join}->_schema[$this->{$join}->primaryKey]['type'] === 'string' ||
  18.                         $this->{$join}->_schema[$this->{$join}->primaryKey]['type'] === 'binary'
  19.                     )
  20.                 );
  21.  
  22.                 $newData = $newValues = array();
  23.                 $primaryAdded = false;
  24.  
  25.                 $fieldsarray(
  26.                     $db->name($this->hasAndBelongsToMany[$assoc]['foreignKey']),
  27.                     $db->name($this->hasAndBelongsToMany[$assoc]['associationForeignKey'])
  28.                 );
  29.  
  30.                 $idField = $db->name($this->{$join}->primaryKey);
  31.                 if ($isUUID && !in_array($idField, $fields)) {
  32.                     $fields[] = $idField;
  33.                     $primaryAdded = true;
  34.                 }
  35.  
  36.  
  37.                 $oldLinks = array();
  38.                 $toDeleteLinks = array();
  39.  
  40.                 if ($this->hasAndBelongsToMany[$assoc]['unique']) {
  41.                     $conditions = array_merge(
  42.                         array($join . '.' . $this->hasAndBelongsToMany[$assoc]['foreignKey'] => $id),
  43.                         (array)$this->hasAndBelongsToMany[$assoc]['conditions']
  44.                     );
  45.                     $links = $this->{$join}->find('all', array(
  46.                         'conditions' => $conditions,
  47.                         'recursive' => empty($this->hasAndBelongsToMany[$assoc]['conditions']) ? -1 : 0,
  48.                         'fields' => $this->hasAndBelongsToMany[$assoc]['associationForeignKey']
  49.                     ));
  50.  
  51.                     $associationForeignKey = "{$join}." . $this->hasAndBelongsToMany[$assoc]['associationForeignKey'];
  52.                     $oldLinks = Set::extract($links, "{n}.{$associationForeignKey}");
  53.                    
  54.                     $toDeleteLinks = array();
  55.                    
  56.                     $newLinks = array();
  57.                     foreach((array)$data as $row){
  58.                         $newLink = null;
  59.                        
  60.                         if ((is_string($row) && (strlen($row) == 36 || strlen($row) == 16)) || is_numeric($row)) {
  61.                             $newLink = $row;
  62.                         } elseif (isset($row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
  63.                             if(isset($row[$this->{$join}->primaryKey])){
  64.                                 continue;
  65.                             }
  66.                             $newLink = $row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']];
  67.                         } elseif (isset($row[$join]) && isset($row[$join][$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
  68.                             if(isset($row[$join][$this->{$join}->primaryKey])){
  69.                                 continue;
  70.                             }
  71.                             $newLink = $row[$join][$this->hasAndBelongsToMany[$assoc]['associationForeignKey']];
  72.                         }
  73.                        
  74.                         if(!$newLink){
  75.                             continue;
  76.                         }
  77.                        
  78.                         $newLinks[] = $newLink;
  79.                     }
  80.                     $newLinks = array_unique($newLinks);
  81.                    
  82.                     $toDeleteLinks = array_diff($oldLinks,$newLinks);
  83.                    
  84.                     if (!empty($toDeleteLinks)) {
  85.                    $conditions[$associationForeignKey] = $toDeleteLinks;
  86.                         $db->delete($this->{$join}, $conditions);
  87.                     }
  88.                 }
  89.  
  90.                 foreach ((array)$data as $row) {
  91.                     if ((is_string($row) && (strlen($row) == 36 || strlen($row) == 16)) || is_numeric($row)) {
  92.                         if(in_array($row,$oldLinks)){
  93.                             continue;
  94.                         }
  95.                         $values = array(
  96.                             $db->value($id, $this->getColumnType($this->primaryKey)),
  97.                             $db->value($row)
  98.                         );
  99.                         if ($isUUID && $primaryAdded) {
  100.                             $values[] = $db->value(String::uuid());
  101.                         }
  102.                         $values = implode(',', $values);
  103.                         $newValues[] = "({$values})";
  104.                         unset($values);
  105.                     } elseif (isset($row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
  106.                         if(in_array($row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']],$oldLinks)
  107.                             && !isset($row[$this->{$join}->primaryKey]) ){
  108.                             continue;
  109.                         }
  110.                         $newData[] = $row;
  111.                     } elseif (isset($row[$join]) && isset($row[$join][$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
  112.                         if(in_array($row[$join][$this->hasAndBelongsToMany[$assoc]['associationForeignKey']],$oldLinks)
  113.                              && !isset($row[$join][$this->{$join}->primaryKey]) ){
  114.                             continue;
  115.                         }
  116.                         $newData[] = $row[$join];
  117.                     }
  118.                 }
  119.                
  120.  
  121.                 if (!empty($newData)) {
  122.                     foreach ($newData as $data) {
  123.                         $data[$this->hasAndBelongsToMany[$assoc]['foreignKey']] = $id;
  124.                         $this->{$join}->create($data);
  125.                         $this->{$join}->save();
  126.                     }
  127.                 }
  128.  
  129.                 if (!empty($newValues)) {
  130.                     $fieldsimplode(',', $fields);
  131.                     $db->insertMulti($this->{$join}, $fields, $newValues);
  132.                 }
  133.                
  134.             }
  135.         }
  136.     }
  137.  
Parsed in 0.262 seconds, using GeSHi 1.0.7.14

Modify this Paste