CakePHP:保存 HABTM 数据 MySQL 错误


CakePHP: Saving HABTM data MySQL error

我正在尝试在视图中构建一个表单,该表单将数据保存到 3 个不同的表中,这些表在我的 MySQL 数据库中都具有多对多关系,并且在 CakePHP 模型中具有AndBelongsToMany关系。但是我尝试这样做,但它不起作用,它要么只保存我正在使用的控制器中的数据,要么给我以下 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);
    }
}

我的周计划模型是默认的 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' => '',
        )
    );
}

这是我路线中的一行.php:

// Jeffrey's test page
Router::connect('/jeffrey', array('controller' => 'weekschedules', 'action' => 'jeffrey'));

我想发布我的数据库图的屏幕截图,但由于我是新来的,所以我不能发布图像,所以我会尝试描述它:

有一个包含以下列的周期表:

  • id (INT(
  • 名称 (瓦尔查尔(45((
  • 说明(文本(
  • 开始日期时间
  • (日期时间(
  • 结束日期时间(日期时间(
  • 创建(日期时间(
  • 修改时间(日期时间(

然后有一个包含以下列的periods_weekschedules联接表:

  • period_id(国际(
  • weekschedule_id(国际(

和以下外键:

  • fk_periods_has_weekschedules_periods1: periods_weekschedules.period_id -> periods.id
  • fk_periods_has_weekschedules_weekschedules1: periods_weekschedules.weekschedule_id -> weekschedules.id

然后有一个包含以下列的周计划表:

  • id (INT(
  • 名称 (瓦尔查尔(45((
  • 订单Nr (国际(
  • 奥奇文(瓦尔查尔(45((
  • 创建(日期时间(
  • 修改时间(日期时间(

然后有一个包含以下列的timetables_weekschedules联接表:

  • weekschedule_id(国际(
  • timetable_id(国际(

和以下外键:

  • fk_weekschedules_has_timetables_weekschedules1: timetables_weekschedules.weekschedule_id -> weekschedules.id
  • fk_weekschedules_has_timetables_timetables1: timetables_weekschedules.timetable_id -> timetables.id

最后是包含以下列的时间表表:

  • id (INT(
  • 平日(国际(
  • 开始时间(时间(
  • 结束时间(时间(
  • 创建(日期时间(
  • 修改时间(日期时间(

由于我是 CakePHP 的初学者,我希望这些信息足以帮助您帮助我,提前感谢!杰弗里

如果没有完整的代码并对其进行测试,很难调试如此大的关系。但是,如果一切都失败了,您可以手动从您的

$this->request->data

并手动保存每个模型的结果。

另请查看非常详细的指南:如何在CakePHP中保存HABTM数据。