简体   繁体   English

CakePHP:保存HABTM数据MySQL错误

[英]CakePHP: Saving HABTM data MySQL error

I'm trying to build a form in a view that saves data to 3 different tables that all have a many to many relations in my MySQL database, and hasAndBelongsToMany relations in the CakePHP models. 我正在尝试在视图中构建一个表单,该表单将数据保存到3个不同的表中,这些表在我的MySQL数据库中都具有多对多关系,在CakePHP模型中具有AndAndBelongsToMany关系。 However I try to do this though, it doesn't work, it either only saves the data from the controller I'm working in, or gives me the following MySQL error: 但是我尝试这样做,但是它不起作用,它要么只保存我正在使用的控制器中的数据,要么给我以下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)

This is the form in my View/Weekschedules/jeffrey.ctp (will be properly renamed when it's working): 这是我的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>

When the form inputs are like this, it throws the MySQL error, and when I format the inputs that are not in this controller like Period.0.name it just only saves the Weekschedule data. 当表单输入是这样时,它将引发MySQL错误,而当我格式化不在此控制器中的输入(例如Period.0.name)时,它仅保存Weekschedule数据。

This is how the jeffrey action in Controller/WeekschedulesController.php currently looks like, I only set the Weekschedule id as a test because I want to create a new Weekschedule, but that also doesn't work. 这就是Controller / WeekschedulesController.php中的jeffrey操作当前的样子,我只将Weekschedule id设置为测试,因为我想创建一个新的Weekschedule,但这也不起作用。 I also tried saveAll() and saveAssociated() instead of the save() function and tried 'deep'=>true, nothing works. 我还尝试了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);
    }
}

My Weekschedule model is default CakePHP bake code: 我的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' => '',
        )
    );

}

And this is the line in my routes.php: 这是我的routes.php中的这一行:

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

I wanted to post a screenshot of my database diagram, but since I'm new here I can't post images, so I'll try to describe it: 我想发布数据库图的屏幕截图,但是由于我是新手,所以我无法发布图像,所以我将尝试描述一下:

There's a periods table with the following columns: 有一个周期表,其中包含以下列:

  • id (INT) ID(INT)
  • name (VARCHAR(45)) 名称(VARCHAR(45))
  • description (TEXT) 说明(文字)
  • startdatetime (DATETIME) 开始日期时间(DATETIME)
  • enddatetime (DATETIME) 结束日期时间(DATETIME)
  • created (DATETIME) 已创建(DATETIME)
  • modified (DATETIME) 已修改(DATETIME)

Then there's a periods_weekschedules join table with the following columns: 然后是一个带有以下各列的Periods_weekschedules连接表:

  • period_id (INT) period_id(INT)
  • weekschedule_id (INT) weekchedule_id(INT)

And the following foreign keys: 以及以下外键:

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

Then there's a weekschedules table with the following columns: 然后是带有以下各列的weekschedules表:

  • id (INT) ID(INT)
  • name (VARCHAR(45)) 名称(VARCHAR(45))
  • orderNr (INT) orderNr(INT)
  • oddeven (VARCHAR(45)) 单数(VARCHAR(45))
  • created (DATETIME) 已创建(DATETIME)
  • modified (DATETIME) 已修改(DATETIME)

Then there's a timetables_weekschedules join table with the following columns: 然后是一个带有以下列的timetables_weekschedules连接表:

  • weekschedule_id (INT) weekchedule_id(INT)
  • timetable_id (INT) timetable_id(INT)

And the following foreign keys: 以及以下外键:

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

And finally a timetables table with the following columns: 最后是包含以下各列的时间表表:

  • id (INT) ID(INT)
  • weekday (INT) 工作日(INT)
  • starttime (TIME) 开始时间(TIME)
  • endtime (TIME) 结束时间(TIME)
  • created (DATETIME) 已创建(DATETIME)
  • modified (DATETIME) 已修改(DATETIME)

Since I'm a beginner with CakePHP, I hope this is enough information to help you help me, thanks in advance! 由于我是CakePHP的初学者,所以我希望此信息足以为您提供帮助,在此先感谢您! Jeffrey 杰弗里

Its hard to debug such big relations without having the full code and testing it. 如果没有完整的代码并对其进行测试,则很难调试如此大的关系。 But if everything fails you can manually sort the data from your 但是,如果一切都失败了,您可以手动从

$this->request->data

and manually save the results for each model. 并手动保存每个模型的结果。

Also take a look at the very detailed guide: How to save HABTM data in CakePHP 还可以看一下非常详细的指南: 如何在CakePHP中保存HABTM数据

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

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