1

我应该做一个多重列表。其中两个字段具有相同的共同值。例如,如果我进入国际米兰、米兰、尤文,他们将拥有 nationality_season 和 series_season 作为常用字段。此外,用户应该决定要制作的广告的数量。

只有当我插入所有内容时:错误:SQLSTATE [HY000]:一般错误:1364 字段“club_id”没有默认值

在此处输入图像描述

我有这个数据库

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);

在此处输入图像描述

管理插入的函数是锦标赛控制器中的 add 函数:

 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>
4

1 回答 1

0

一方面,您的表单控件的字段名称与您的数据库架构为nationality_championship和显示的字段名称不同championship_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控件命名为clubs._ids,您的原始补丁和保存代码就可以正常工作以创建整个记录结构。

于 2019-12-24T17:08:18.263 回答