我应该做一个多重列表。其中两个字段具有相同的共同值。例如,如果我进入国际米兰、米兰、尤文,他们将拥有 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>