0

我正在尝试从多项选择中保存数据。该数据在“请求”有许多“请求c”的地方被重新定义。foriegnKey 是“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“字段列表”中的未知列“数组”

SQL 查询: INSERT INTO societariorequests_certidoes( caminho, request_id) 值(数组,62)

谢谢大家

4

1 回答 1

2

您需要修改发布的数据,使其看起来像这样:

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;

$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存储idof Certificates,这似乎是一个 HABTM 关系; Request --> HABTM --> Certificate,在这种情况下,应该调用连接表certificates_requests并包含列request_idcertificate_id。请参阅模型和数据库约定

于 2013-04-10T22:14:10.487 回答