CakePHP - 多行编辑 [英] CakePHP - Multiple row edits

查看:89
本文介绍了CakePHP - 多行编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对多行上的表进行编辑?

我按照此处的教程 http://matsimitsu.com/blog/2008/01/06/saveall-with-cakephp.html ,但似乎无效。



这是我在做什么,但它不工作。



感谢,

Tee

  function editAll(){
$ this-> data = $ this-> Settings-> find('all',array('conditions'=> $ conditions));
}

然后在视图中,这是我有

  foreach($ this-> data as $ setting):
echo $ form-> input('Setting。'。$ setting ['setting'] [id]。'.value',array('value'=> $ setting ['Setting'] [value])));
endforeach;

然后在添加函数中我有

  function add(){
if(!empty($ this-> data)){
$ this-> Setting-> create
if($ this-> Setting-> saveAll($ this-> data)){
$ this-> Session-> setFlash(__(' ,true));
$ this-> redirect(array('action'=>'index'));
} else {
$ this-> Session-> setFlash(__('无法保存设置,请重试。
}
}
}


解决方案>

您需要包含 id 字段,因此控制器中的数据将如下所示:

 'Setting'=> array(
0 => array(
'id'=> 42,
'value'=>'foo'
),
1 = ; array(...)

所以在视图中, >

  foreach($ this-> data as $ i => $ setting){
echo $ this-> Form - > hidden(Setting。$ i.id,array('value'=> $ setting ['Setting'] ['id'])));
echo $ this-> Form-> input(Setting。$ i.value,array('value'=> $ setting ['Setting'] ['value'])));
}


How can I do edits to a table on multiple rows?
I followed the tutorial here http://matsimitsu.com/blog/2008/01/06/saveall-with-cakephp.html but it doesn't seem to work.

Here is what I'm doing, but it's not working.

Thanks,
Tee

function editAll() {
    $this->data = $this->Settings->find('all', array('conditions' => $conditions));
}

Then in the view, this is what I have

foreach ($this->data as $setting):
    echo $form->input('Setting.' . $setting['Setting']["id"] . '.value', array('value' => $setting['Setting']["value"]));
endforeach;

Then in the add function I have

function add() {
    if (!empty($this->data)) {
        $this->Setting->create();
        if ($this->Setting->saveAll($this->data)) {
            $this->Session->setFlash(__('The Setting has been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The Setting could not be saved. Please, try again.', true));
        }
    }
}

解决方案

You need to include the id field, so the data in your controller will look like this:

'Setting' => array(
    0 => array(
        'id' => 42,
        'value' => 'foo'
    ),
    1 => array(…)
)

So in the view, do this:

foreach ($this->data as $i => $setting) {
    echo $this->Form->hidden("Setting.$i.id", array('value' => $setting['Setting']['id']));
    echo $this->Form->input("Setting.$i.value", array('value' => $setting['Setting']['value']));
}

这篇关于CakePHP - 多行编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆