问题
I'm trying to save a data from a multiple select. This data is relacioned where "Request" hasMany "Requestc". The foriegnKey is "request_id"
My Controller:
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 is my data from $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'
)
)
And that's the error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Array' in 'field list'
SQL Query: INSERT INTO societario
.requests_certidoes
(caminho
, request_id
) VALUES (Array, 62)
Thanks for all
回答1:
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',
)
)
)
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);
See the documentation: Saving Related Model Data (hasOne, hasMany, belongsTo)
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
来源:https://stackoverflow.com/questions/15937068/trying-to-save-a-hasmany-array-from-multiple-select