私は、MySQLデータベースで多対多のリレーションがあり、CakePHPモデルではhasAndBelongsToManyリレーションがある3つの異なるテーブルにデータを保存するビューでフォームを作成しようとしています。
ただし、これを実行しようとすると、機能しません。機能しているコントローラーのデータが保存されるか、次の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を作成したいので、Weekschedule IDをテストとして設定するだけですが、それも機能しません。また、save()関数の代わりにsaveAll()とsaveAssociated()を試し、「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'));
データベースダイアグラムのスクリーンショットを投稿したかったのですが、ここに来たばかりなので画像を投稿できません。そのため、次のように説明します。
次の列を持つ期間テーブルがあります。
id(INT)
名前(VARCHAR(45))
説明(TEXT)
startdatetime(DATETIME)
enddatetime(DATETIME)
作成(DATETIME)
変更(DATETIME)
次に、次の列を含むperiod_weekschedules結合テーブルがあります。
period_id(INT)
weekschedule_id(INT)
そして、次の外部キー:
fk_periods_has_weekschedules_periods1:periods_weekschedules.period_id-> period.id
fk_periods_has_weekschedules_weekschedules1:period_weekschedules.weekschedule_id-> weekschedules.id
次に、次の列が含まれるWeekschedulesテーブルがあります。
id(INT)
名前(VARCHAR(45))
orderNr(INT)
奇数(VARCHAR(45))
作成(DATETIME)
変更(DATETIME)
次に、次の列を持つtimetables_weekschedules結合テーブルがあります。
weekschedule_id(INT)
timetable_id(INT)
そして、次の外部キー:
fk_weekschedules_has_timetables_weekschedules1:timetables_weekschedules.weekschedule_id-> weekschedules.id
fk_weekschedules_has_timetables_timetables1:timetables_weekschedules.timetable_id-> timetables.id
そして最後に、次の列を持つtimetablesテーブル:
id(INT)
平日(INT)
開始時間(TIME)
終了時間(TIME)
作成(DATETIME)
変更(DATETIME)
私はCakePHPの初心者なので、これがあなたを助けるのに十分な情報であることを願っています。
ジェフリー