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

Modify this Paste