尝试从多个select保存hasMany数组 [英] Trying to save a hasMany array from multiple select
问题描述
我正在尝试从多个选择中保存数据。此数据在请求有多个请求的地方。 foriegnKey是request_id
I'm trying to save a data from a multiple select. This data is relacioned where "Request" hasMany "Requestc". The foriegnKey is "request_id"
我的控制器:
if ($this->request->is('post')) {
$solicitacao = $this->Request->save($this->request->data['Request']);
//Verifica se a request foi salva e se sim, salva quais as certidões foram pedidas na tabela requests_certidoes
if(!empty($solicitacao)) {
$this->request->data['Requestc']['request_id'] = $this->Request->id;
// debug($this->request->data);
$this->Request->Requestc->saveAll($this->request->data);
}
}
这是我的数据 $ this-> request-> data
:
array(
'Request' => array(
'motivo' => 'Licitação',
'nome_licitacao' => '',
'data_pregao' => '',
'nome_cliente' => '',
'outros' => ''
),
'Requestc' => array(
'caminho' => array(
(int) 0 => '1',
(int) 1 => '3'
),
'request_id' => '60'
)
)
这是错误:
错误:SQLSTATE [42S22]:未找到列:1054字段列表中的未知列Array
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'
SQL查询:INSERT INTO societario
。 requests_certidoes
( caminho
,
request_id
)VALUES(Array,62)
SQL Query: INSERT INTO societario
.requests_certidoes
(caminho
, request_id
) VALUES (Array, 62)
感谢所有
推荐答案
您需要修改发布的数据,如下所示:
You need to modify the posted data so that it looks like this:
array(
'Request' => array(
'motivo' => 'Licitação',
'nome_licitacao' => '',
'data_pregao' => '',
'nome_cliente' => '',
'outros' => ''
),
'Requestc' => array(
0 => array(
'caminho' => '1',
// --> optionally add your request_id here
// if you're manually saving Requestc
// AFTER saving Request
),
1 => array(
'caminho' => '3',
)
)
)
如果您的关系设置正确,必须添加request_id;
If your relations are properly set-up, you probably don't have to add request_id;
$data = array(
'Request' => $this->request->data['Request'],
'Requestc' => array();
);
foreach($this->request->data['Requestc']['caminho'] as $val) {
$data['Requestc'][] = array(
'caminho' => $val,
// Should NOT be nescessary when using the saveAssociated()
// as below
//'request_id' => $this->Request->id;
);
}
// This should insert both the Request *and* the Requestc records
$this->Request->saveAssociated($data);
请参阅文档:保存相关模型数据(hasOne,hasMany,belongsTo)
但是,如果 Requestc.caminho
存储 id
c $ c> Certificates ,这似乎是一个HABTM关系; 请求 - > HABTM - >证书
,在这种情况下,连接表应该被调用 certificates_requests
并包含 request_id
和 certificate_id
。请参阅模型和数据库约定
However, if Requestc.caminho
stores the id
of Certificates
, this seems to be a HABTM relation; Request --> HABTM --> Certificate
, in which case the join-table should be called certificates_requests
and contain the columns request_id
and certificate_id
. See the Model and Database Conventions
这篇关于尝试从多个select保存hasMany数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!