在Cakephp 3.8中多次插入

时间:2019-12-23 15:11:36

标签: php mysql cakephp cakephp-3.8

我应该列出多个清单。其中两个字段具有相同的相同值。例如,如果我输入国际米兰,米兰,尤文,他们将把national_season和series_season作为共同字段。此外,用户应决定要制作的广告数量。

仅当我要插入所有内容时:错误:SQLSTATE [HY000]:常规错误:1364字段'club_id'没有默认值

enter image description here

我有这个数据库

DROP TABLE IF EXISTS `championships`;
CREATE TABLE IF NOT EXISTS `championships` (
  `id` int(11),
  `club_id` int(11) NOT NULL,
  `season` varchar(9) NOT NULL DEFAULT ' ',
  `nationality_championship` varchar(50) NOT NULL DEFAULT '0',
  `championship_series` varchar(50) NOT NULL DEFAULT '0',
  `penal` int(11) DEFAULT '0',
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `clubs`;
CREATE TABLE IF NOT EXISTS `clubs` (
  `id` int(11),
  `name` varchar(35) NOT NULL DEFAULT ' '
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

ALTER TABLE clubs
ADD PRIMARY KEY (id),
MODIFY id int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE championships
ADD PRIMARY KEY (id),
MODIFY id int(11) NOT NULL AUTO_INCREMENT,
ADD FOREIGN KEY(club_id) REFERENCES clubs(id);

enter image description here

管理插入的功能是冠军控制器中的添加功能:

 public function add()
    {

        $championship = $this->Championships->newEntity();
        if ($this->request->is('post')) {
            $championship = $this->Championships->patchEntity($championship, $this->request->getData());
            if ($this->Championships->save($championship)) {
                $this->Flash->success(__('The championship has been saved.'));

                return $this->redirect(['action' => 'index']);
            }
            $this->Flash->error(__('The championship could not be saved. Please, try again.'));
        }

        $clubsUnmated=$this->Championships->Clubs->find('list',['keyField' => 'id',
        'valueField' =>['nome_societa']])->notMatching('Championships');

        $this->set(compact('championship', 'clubsUnmated'));
    }

提取代码add.ctp:

<div class="championships form large-9 medium-8 columns content">
    <?= $this->Form->create($championship) ?>
    <fieldset>
        <legend><?= __('Add Championship') ?></legend>
        <?php

            echo $this->Form->control('season',['class'=>'form-control']);
            echo $this->Form->control('nazionalità_campionato',['class'=>'form-control']);
            echo $this->Form->control('serie_campionato',['class'=>'form-control']);
            echo $this->Form->control('club_id', ['options' => $clubsUnmated,'data-role'=>'tagsinput','type'=>'text','placeholder'=>'aggiungi squadre','class'=>'form-control']);

        ?>
    </fieldset>
    <?= $this->Form->button(__('Submit')) ?>
    <?= $this->Form->end() ?>
</div>

1 个答案:

答案 0 :(得分:0)

一方面,表单控件的字段名称与nationality_championshipchampionship_series的数据库模式所显示的字段名称不同。那不是引起这个问题,但是很快就会成为问题。

最主要的是,看起来您将从club_id中获取值数组,但是数据库中的字段是单个值。如果要坚持使用该数据库架构,则必须遍历该数组(在表单中可能应称为其他名称,以避免在创建要保存的实体时出现问题)。例如,如果将该控件重命名为clubs,则它可能看起来像这样:

if ($this->request->is('post')) {
    $championships = [];
    $data = $this->request->getData();
    foreach ($data['clubs'] as $club_id) {
        $championships[] = $this->Championships->newEntity(array_merge($data, compact('club_id')));
    }
    if ($this->Championships->saveMany($championships)) {
        $this->Flash->success(__('The championship has been saved.'));
        return $this->redirect(['action' => 'index']);
    }
    $this->Flash->error(__('The championship could not be saved. Please, try again.'));
}

但是,即使那样也可能不是您真正想要的,因为它在数据库中多次重复了冠军数据。我认为您真正想要的是根本不在该表中拥有club_id,而是拥有一个championships_clubs连接表,然后您可以将club_id控件命名为{{ 1}},并且原始的补丁程序和保存代码可以很好地创建整个记录结构。