【问题标题】:multiple insertion in cakephp 3.8cakephp 3.8 中的多次插入
【发布时间】:2020-04-14 20:54:18
【问题描述】:

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

管理插入的函数是冠军控制器中的添加函数:

 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>

【问题讨论】:

    标签: php mysql cakephp cakephp-3.8


    【解决方案1】:

    一方面,您的表单控件的字段名称与数据库架构为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 控件命名为clubs._ids 和您的原始补丁和保存代码可以很好地创建整个记录结构。

    【讨论】:

    • 是的,这些字段是意大利语的,我翻译了它们以使它们更容易被社区理解。所以名字是正确的,只是一个错字。对不起
    • 我想做的是在锦标赛表中进行多次添加。基本上我想在表格中添加 N 个 ennuple,我每次都重复一些字段。 [1]:i.stack.imgur.com/TvPPp.png 它提供了一个我想做的例子
    • 这应该通过我提供的代码来完成。但是让冠军数据重复这么多次是糟糕的数据库设计。当您意识到自己犯了一个错误并需要更新 10 行时会发生什么?
    • 什么才是正确的数据库设计?再次感谢您的支持。问题是这样的:每个俱乐部每年只能报名参加一个联赛。多年来,我们需要跟踪各种联赛。所以每个俱乐部每年都会创建一个新联赛
    • 正如我所说,你真正想要的根本不是在那个表中有club_id,而是有一个championships_clubs 连接表。
    猜你喜欢
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 2013-09-10
    • 2016-04-28
    • 1970-01-01
    相关资源
    最近更新 更多