Mysql Cakephp 3.x保存一对一关系
我正在使用mySQL开发cakephp。我定义了一对一关系: 一个Camada有一个Grupo- 我设计了一个表单,用户在其中输入两个实体的信息 我试图保存关系,但是camada实体从来没有GRUPO_id的引用,它不允许继续存在 以下是我的CamadasController中的代码:Mysql Cakephp 3.x保存一对一关系,mysql,cakephp-2.0,one-to-one,Mysql,Cakephp 2.0,One To One,我正在使用mySQL开发cakephp。我定义了一对一关系: 一个Camada有一个Grupo- 我设计了一个表单,用户在其中输入两个实体的信息 我试图保存关系,但是camada实体从来没有GRUPO_id的引用,它不允许继续存在 以下是我的CamadasController中的代码: public function add() { $camada = $this->Camadas->newEntity(); if ($this->request-&g
public function add() {
$camada = $this->Camadas->newEntity();
if ($this->request->is('post')) {
$camada = $this->Camadas->patchEntity($camada, $this->request->data, [
'associated' => [
'Grupos'
]
]);
$camada->grupos = TableRegistry::get('Grupos')->newEntity();
$camada->usuario_creacion = 2;
$camada->fecha_creacion = date("d/m/Y");
$camada->eliminado = 0;
$camada->grupos->eliminado = 0;
$camada->grupos->fecha_creacion = Time::now();
$camada->usuario_creacion = 2;
if ($this->Camada->saveAssociated($camada)) {
$this->Flash->success(__('La camada fue guardada'));
return $this->redirect(['action' => 'index']);
}
else {
$this->Flash->error(__('La camada no pudo ser guardada. Por favor, intente nuevamente'));
}
}
$this->set(compact('camada'));
$this->set('_serialize', ['camada']);
}
我得到的错误如下:
对布尔函数调用成员函数saveAssociated()
第行:if($this->Camada->saveAssociated($Camada)){
[更新:更改后]
public function add() {
$camada = $this->Camadas->newEntity();
if ($this->request->is('post')) {
$camada = $this->Camadas->patchEntity($camada, $this->request->data, [
'associated' => [
'Grupos'
]
]);
$camada->grupos = TableRegistry::get('Grupos')->newEntity();
$camada->usuario_creacion = 2;
$camada->fecha_creacion = date("d/m/Y");
$camada->eliminado = 0;
$camada->grupos->eliminado = 0;
$camada->grupos->fecha_creacion = Time::now();
$camada->grupos->usuario_creacion = 2;
if ($this->Camadas->save($camada)) {
echo $camada->grupos_id;
$this->Flash->success(__('La camada fue guardada'));
return $this->redirect(['action' => 'index']);
}
else {
$this->Flash->error(__('La camada no pudo ser guardada. Por favor, intente nuevamente'));
}
}
$this->set(compact('camada'));
$this->set('_serialize', ['camada']);
}
错误:
错误:SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(kick
camadas
,约束camadas\u ibfk\u 1
外键(grupos\u id
)引用grupos
(id
)在更新级联上)
对象(App\Model\Entity\Camada){
你完全做错了..你正在使用哪个版本的cakephp?你正在将cakephp 2和cakephp 3混合在一起..好..刚刚检查过,它是3.3.13,然后简单地使用save而不是saveAssociated..我已经尝试过了。-刚刚更改了saveAssociated并使用了save。相同的错误:在布尔值上调用成员函数save()
'grupo' => object(App\Model\Entity\Grupo) {
'nombre' => 'test',
'contrato' => 'test',
'eliminado' => (int) 0,
'fecha_creacion' => object(Cake\I18n\Time) {},
'usuario_creacion' => (int) 2,
'[new]' => true,
'[accessible]' => [
[maximum depth reached]
],
'[dirty]' => [
[maximum depth reached]
],
'[original]' => [[maximum depth reached]],
'[virtual]' => [[maximum depth reached]],
'[errors]' => [[maximum depth reached]],
'[invalid]' => [[maximum depth reached]],
'[repository]' => 'Grupos'
},
'descripcion' => 'test',
'año' => (int) 29837,
'contacto1' => 'lkjsdf',
'contacto2' => 'kjfsdf',
'usuario_creacion' => (int) 2,
'fecha_creacion' => '06/02/2017',
'eliminado' => (int) 0,
'created' => object(Cake\I18n\Time) {
'time' => '2017-02-06T05:26:21+00:00',
'timezone' => 'UTC',
'fixedNowTime' => false
},