繁体   English   中英

CakePHP:保存HABTM数据MySQL错误

[英]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'));

我想发布数据库图的屏幕截图,但是由于我是新手,所以我无法发布图像,所以我将尝试描述一下:

有一个周期表,其中包含以下列:

  • ID(INT)
  • 名称(VARCHAR(45))
  • 说明(文字)
  • 开始日期时间(DATETIME)
  • 结束日期时间(DATETIME)
  • 已创建(DATETIME)
  • 已修改(DATETIME)

然后是一个带有以下各列的Periods_weekschedules连接表:

  • period_id(INT)
  • weekchedule_id(INT)

以及以下外键:

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

然后是带有以下各列的weekschedules表:

  • ID(INT)
  • 名称(VARCHAR(45))
  • orderNr(INT)
  • 单数(VARCHAR(45))
  • 已创建(DATETIME)
  • 已修改(DATETIME)

然后是一个带有以下列的timetables_weekschedules连接表:

  • weekchedule_id(INT)
  • timetable_id(INT)

以及以下外键:

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

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

  • ID(INT)
  • 工作日(INT)
  • 开始时间(TIME)
  • 结束时间(TIME)
  • 已创建(DATETIME)
  • 已修改(DATETIME)

由于我是CakePHP的初学者,所以我希望此信息足以为您提供帮助,在此先感谢您! 杰弗里

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

$this->request->data

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

还可以看一下非常详细的指南: 如何在CakePHP中保存HABTM数据

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM