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
任何幫助是極大的讚賞試過!謝謝!
感謝您的回覆,Patrick。 –
如果我的問題可以幫助你解決問題,請不要忘記投票並標記解決。 –
感謝您的回覆,Patrick。 稍有幫助,仍然有點卡住。 現在它仍然創建2條記錄,但是第一條記錄的'event_id'設置爲null'promoter_id',第二條記錄的空'event_id'和正確的'promot_id'我仍然在使用它,將很快更新 –