CakePHP保存多个数据

问题描述 投票:0回答:1

我写了一个自定义视图,看起来像这样:http://img405.imageshack.us/i/taula.jpg/

这是我的控制器功能,目前尚未实现保存数据:http://pastebin.com/cU5rprFB

这是我的观点:http://pastebin.com/4bYLPp4z

我以为这样写形式归因于:

<td>
<input name="data[Linea][0][proyecto_id]" type="hidden" value=" <?php echo $proyecto['Proyecto']['id'] ?>" />
<input name="data[Linea][0][hito_id]" type="hidden" value=" <?php echo $proyecto['Hito']['id'] ?>" />
<input name="data[Linea][0][tarea_id]" type="hidden" value=" <?php echo $proyecto['Tarea']['id'] ?>" />
<input name="data[Linea][0][total_horas]" type="text" id="LineaTotalHotas" value="" >
</td>

足够...但是还不够。在debug_kit中,我看到隐藏的数据还可以,但是输入中的数据丢失了...

有人有什么榜样或帮助我的东西吗?


更新:我正在控制器中尝试类似的操作:

function addhoras() {
    if (!empty($this->data)) {
        xdebug_break();
        foreach($this->data['Linea'] as $l) {
            if ( ($l['total_horas'] != 0) && ( $l['total_horas']!=NULL ) ) {
                $this->Linea->create();
                if ( $this->Linea->save($l) ) {

                } else {
                    $this->Session->setFlash(__('Arazonbat eon dek', true));
                }
            }
        }
        //$this->redirect(array('action' => 'addhoras'));
    }

但是我在if ( $this->Linea->save($l) )行上得到了setflash ...所以它没有保存任何数据...这是var_dump($l)

array
'proyecto_id' => string ' 1' (length=2)

'hito_id' => string ' 3' (length=2)

'usuario_id' => string ' 1' (length=2)

'fecha' => string '2011-01-01 ' (length=11)

'total_horas' => string '33' (length=2)
model-view-controller cakephp model view
1个回答
1
投票

请确保您正确构建了数组http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM

并且如果要保存相关数据,请记住使用saveAll而不是save。

请确保传递给“保存”方法的数组具有典型的Cake结构。就像这样:

array
(
    [Modelo] => array
    (
         [celda] => valor
    )
)

并且请记住,如果要保存相关数据(HasMany或HasAndBelongsToMany ..,则必须使用“ saveAll”而不是“ save”]

http://book.cakephp.org/view/83/hasAndBelongsToMany-HABTM

© www.soinside.com 2019 - 2024. All rights reserved.