我有这个错误:
无法插入行,缺少部分主键值。拿到 (, 16,19),期待(id,passenger_id,invoices_id
现在我无法插入字段,因为不要生成ID,在我的表格中我将其视为自动增量。
public function printInvoice($id = null)
{
$this->layout = 'invoice';
$this->loadModel('Travels');
$invoices = $this->Tickets->getInvioce();
if(!($this->Tickets->existPassengerTicket($id))){
$ticket = $this->Tickets->newEntity();
$ticket->passenger_id = $id;
$ticket->invoices_id = $invoices->id;
$ticket->date = date('Y-m-d H:i:s');
$ticket = $this->Tickets->patchEntity($ticket, $this->request->data);
$this->Tickets->save($ticket);
}else{
}
//$passengers = $this->Tickets->getPassenger($id);
//$invoices = $this->Tickets->getInvioce();
//$idTravel=$passengers->travel_id;
//$travels = $this->Travels->getTravel($idTravel);
//$this->set(compact('ticket', 'passengers', 'invoices', 'travels'));
//$this->set('_serialize', ['ticket']);
}
这里是SHOW CREATE TABLE门票:
| tickets | CREATE TABLE `tickets` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`passenger_id` int(11) NOT NULL,
`invoice_id` int(11) NOT NULL,
`date` datetime DEFAULT NULL,
`update` datetime DEFAULT NULL,
PRIMARY KEY (`id`,`passenger_id`,`invoice_id`),
KEY `fk_tickets_passengers1_idx` (`passenger_id`),
KEY `fk_tickets_invoices1_idx` (`invoice_id`),
CONSTRAINT `fk_tickets_invoices1` FOREIGN KEY (`invoice_id`) REFERENCES `invoices` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_tickets_passengers1` FOREIGN KEY (`passenger_id`) REFERENCES `passengers` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
答案 0 :(得分:1)
我认为你应该检查In Model / Entity / Tickets.php。当id,passenger_id,false时我得到了这个错误,我将其改为true就可以了。
protected $_accessible = [
'*' => true,
'id'=> false,
'passenger_id' => false,
'invoices_id' => false,
];
我认为当你将false改为true时你会好的。
protected $_accessible = [
'*' => true,
'id'=> true,
'passenger_id' => true,
'invoices_id' => true,
];
答案 1 :(得分:0)
检查您的表类并查看validationDefault
函数。
$validator
->add('id', 'valid', ['rule' => 'numeric'])
->allowEmpty('id', 'create')
// your other code here
return $validator;
确保至少有两行代码引用了id的创建。我也会检查Entity对象,以防万一。
答案 2 :(得分:0)
“无法插入行,缺少部分主键值。得到(1,),期待(id,something_id)”
通过编辑 ExampleTable.php :
解决了这个错误此代码导致错误:
$this->primaryKey(['id', 'something_id']);
这就是它的工作原理:
$this->primaryKey('id');
我使用mysql workbench创建了数据库,然后进行了蛋糕烘焙。所以,如果你喜欢我,我上面显示的解决方法也可能对你有用。