2013-10-03 107 views
0

我遇到一個問題,CakePHP忽略保存指定的字段,而是創建額外的記錄。我正在使用CakePHP 2.3.6。CakePHP SaveAssociated/SaveAll忽略字段

我的表看起來像這樣:

CREATE TABLE `events_guests` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `guest_id` int(11) NOT NULL, 
    `event_id` int(11) NOT NULL, 
    `promoter_id` int(11) DEFAULT NULL, 
    `created` datetime NOT NULL, 
    `modified` datetime NOT NULL, 
    `attended` varchar(15) NOT NULL DEFAULT 'No', 
    PRIMARY KEY (`id`) 
) 

繼承人的代碼

public function addGuest($event_id, $promoter_id = null) { 
if ($this->request->is('post')) { 
       $this->Guest->create(); 
       $event_data = array('event_id' => $event_id); 
       $data = $this->request->data; 
       $data['Event'] = $event_data; 
       if($promoter_id) { 
        $data['Event']['promoter_id'] = $promoter_id; 
       } 
       if ($this->Guest->saveAssociated($data)) { 
        $this->Session->setFlash(__('The guest has been added to the guestlist'), 'flash/success'); 
       } else { 
       $this->Session->setFlash(__('The guest could not be saved. Please, try again.'), 'flash/error'); 
       } 

這裏是我的數據,我想救:

Array (
    [Guest] => Array 
    (
     [first_name] => Joe 
     [last_name] => Schmoe 
    ) 
    [Event] => Array 
    (
     [event_id] => 1 
     [promoter_id] => 2 
    ) 
) 

我的模型如下:

class Guest extends AppModel { 
public $hasAndBelongsToMany = array(
     'Event' => array(
      'className' => 'Event', 
      'joinTable' => 'events_guests', 
      'foreignKey' => 'guest_id', 
      'associationForeignKey' => 'event_id', 
      'unique' => 'true', 
     ), 
     'Promoter' => array(
      'className' => 'Promoter', 
      'joinTable' => 'events_guests', 
      'foreignKey' => 'guest_id', 
      'associationForeignKey' => 'promoter_id', 
      'unique' => 'true', 
     ) 
    ); 
} 

而且

class Event extends AppModel { 
public $hasAndBelongsToMany = array(
     'Guest' => array(
      'className' => 'Guest', 
      'joinTable' => 'events_guests', 
      'foreignKey' => 'event_id', 
      'associationForeignKey' => 'guest_id', 
      'unique' => 'keepExisting', 
      'order' => 'last_name', 
     ), 
     'Promoter' => array(
      'className' => 'Promoter', 
      'joinTable' => 'events_promoters', 
      'foreignKey' => 'event_id', 
      'associationForeignKey' => 'promoter_id', 
      'unique' => 'keepExisting', 
     ) 
    ); 
} 

從這個結果是2個記錄EventsGuests,既不用promoter_id

一個記錄接收event_id = 1如預期

其他記錄接收event_id = 2,這實際上是promoter_id

我一直在使用的saveAssociated混合和saveAll

任何幫助是極大的讚賞試過!謝謝!

回答

0

參考:Cakephp save extra attribute in HABTM relation

對於您的問題,不使用HABTM,因爲,HABTM通常用於連接2個表

因此,如果您有超過2個字段,請創建一個模型並使用它,如belongsTo和hasMany的關係。

class Event extends AppModel { 

    $hasMany = array(
    'EventGuest' => array(
     'className' => 'EventGuest', 
     'foreignKey' => 'event_id'  
    ) 
); 

} 

class Guest extends AppModel { 

    $hasMany = array(
    'EventGuest' => array(
     'className' => 'EventGuest', 
     'foreignKey' => 'guest_id'  
    ) 
); 

} 

class Promoter extends AppModel { 

    $hasMany = array(
    'EventGuest' => array(
     'className' => 'EventGuest', 
     'foreignKey' => 'promoter_id'  
    ) 
); 

} 

現在,你的關係模型。

class EventGuest extends AppModel { 

    $belongsTo = array(
    'Event' => array(
     'className' => 'Event', 
     'foreignKey' => 'event_id' 
    ), 
    'Guest' => array(
     'className' => 'Guest', 
     'foreignKey' => 'guest_id' 
    ), 
    'Promoter' => array(
     'className' => 'Promoter', 
     'foreignKey' => 'promoter_id' 
    ) 
); 

} 

並繼續使用您的代碼。

public function addGuest($event_id, $promoter_id = null) { 
    if ($this->request->is('post')) { 
    $this->Guest->create(); 
    $event_data = array('event_id' => $event_id); 
    $data = $this->request->data; 
    $data['Event'] = $event_data; 
    if($promoter_id) { 
     // EDITED 
     $data['Promoter']['promoter_id'] = $promoter_id; 
    } 

    if ($this->Guest->saveAssociated($data)) { 
     $this->Session->setFlash(__('The guest has been added to the guestlist'), 'flash/success'); 
    } else { 
     $this->Session->setFlash(__('The guest could not be saved. Please, try again.'), 'flash/error'); 
    } 
    } 
} 
+0

感謝您的回覆,Patrick。 –

+0

如果我的問題可以幫助你解決問題,請不要忘記投票並標記解決。 –

+0

感謝您的回覆,Patrick。 稍有幫助,仍然有點卡住。 現在它仍然創建2條記錄,但是第一條記錄的'event_id'設置爲null'promoter_id',第二條記錄的空'event_id'和正確的'promot_id'我仍然在使用它,將很快更新 –