[英]CakePHP: Saving HABTM data MySQL error
我正在嘗試在視圖中構建一個表單,該表單將數據保存到3個不同的表中,這些表在我的MySQL數據庫中都具有多對多關系,在CakePHP模型中具有AndAndBelongsToMany關系。 但是我嘗試這樣做,但是它不起作用,它要么只保存我正在使用的控制器中的數據,要么給我以下MySQL錯誤:
Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`agenda`.`timetables_weekschedules`, CONSTRAINT `fk_weekschedules_has_timetables_timetables1` FOREIGN KEY (`timetable_id`) REFERENCES `timetables` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTIO)
這是我的View / Weekschedules / jeffrey.ctp中的表格(在工作時將正確重命名):
<div class="form">
<?php echo $this->Form->create('Weekschedule'); ?>
<fieldset>
<legend><?php echo __('Create workschedule'); ?></legend>
<?php
echo "<h1>Period</h1>";
echo $this->Form->input('Period.name');
echo $this->Form->input('Period.description');
echo $this->Form->input('Period.startdatetime');
echo $this->Form->input('Period.enddatetime');
echo "<h1>Weekschedule</h1>";
echo $this->Form->input('Weekschedule.name');
echo $this->Form->input('Weekschedule.oddeven');
echo "<h1>Timetable</h1>";
echo $this->Form->input('Timetable.weekday');
echo $this->Form->input('Timetable.starttime');
echo $this->Form->input('Timetable.endtime');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
當表單輸入是這樣時,它將引發MySQL錯誤,而當我格式化不在此控制器中的輸入(例如Period.0.name)時,它僅保存Weekschedule數據。
這就是Controller / WeekschedulesController.php中的jeffrey操作當前的樣子,我只將Weekschedule id設置為測試,因為我想創建一個新的Weekschedule,但這也不起作用。 我還嘗試了saveAll()和saveAssociated()而不是save()函數,並嘗試了'deep'=> true,沒有任何效果。
public function jeffrey() {
$this->Weekschedule->recursive = 2;
$this->request->data['Weekschedule']['id'] = 95;
debug($this->request->data);
if (!empty($this->request->data)) {
$this->Weekschedule->save($this->request->data);
}
}
我的Weekschedule模型是默認的CakePHP烘焙代碼:
<?php
App::uses('AppModel', 'Model');
/**
* Weekschedule Model
*
* @property Period $Period
* @property Timetable $Timetable
*/
class Weekschedule extends AppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'name';
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* hasAndBelongsToMany associations
*
* @var array
*/
public $hasAndBelongsToMany = array(
'Period' => array(
'className' => 'Period',
'joinTable' => 'periods_weekschedules',
'foreignKey' => 'weekschedule_id',
'associationForeignKey' => 'period_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
),
'Timetable' => array(
'className' => 'Timetable',
'joinTable' => 'timetables_weekschedules',
'foreignKey' => 'weekschedule_id',
'associationForeignKey' => 'timetable_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
}
這是我的routes.php中的這一行:
// Jeffrey's test page
Router::connect('/jeffrey', array('controller' => 'weekschedules', 'action' => 'jeffrey'));
我想發布數據庫圖的屏幕截圖,但是由於我是新手,所以我無法發布圖像,所以我將嘗試描述一下:
有一個周期表,其中包含以下列:
然后是一個帶有以下各列的Periods_weekschedules連接表:
以及以下外鍵:
然后是帶有以下各列的weekschedules表:
然后是一個帶有以下列的timetables_weekschedules連接表:
以及以下外鍵:
最后是包含以下各列的時間表表:
由於我是CakePHP的初學者,所以我希望此信息足以為您提供幫助,在此先感謝您! 傑弗里
如果沒有完整的代碼並對其進行測試,則很難調試如此大的關系。 但是,如果一切都失敗了,您可以手動從
$this->request->data
並手動保存每個模型的結果。
還可以看一下非常詳細的指南: 如何在CakePHP中保存HABTM數據
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.