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