multiple insertion in cakephp 3.8

108 Views Asked by At

I should make a multiple listing. where two fields have the same values in common. For example, if I enter Inter, Milan, Juve they will have nationality_season and series_season as common fields. Furthermore, the user should decide on the number of advertisements to be made.

only that when I go to insert everything: Error: SQLSTATE[HY000]: General error: 1364 Field 'club_id' doesn't have a default value

enter image description here

i have this database

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

the function that manages the insertion is the add function in the championships controller:

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

extract code 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

There are 1 best solutions below

5
On

For one thing, your form controls have different field names than your database schema is showing for nationality_championship and championship_series. That's not causing this problem, but it'll be a problem soon.

The main thing here is that it looks like you're going to be getting an array of values from your club_id, but the field in the database is a single value. If you want to stick with this database schema, you're going to have to loop over that array (which should probably be called something else in your form, to avoid issues when creating your entities to save). For example, if you rename that control to clubs, then it might look something like this:

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.'));
}

But even that probably isn't what you really want, as it's repeating the championship data many times in your database. I think what you really want is not to have club_id in that table at all, but rather have a championships_clubs join table, and then you can probably just name your club_id control as clubs._ids and your original patch and save code will work fine to create the whole record structure.