0

我有一张桌子叫specs. 我需要以一种形式从其中编辑多行并立即保存。

// table structure
specs [id, name, value, owner_id]
// controller part
$data = $this->Spec->find('all'); // skipping the conditions and other sub-code
$data = set::combine(...);
/*
result, keyed using the spec[id] column.
array (
  [3] = array(id=>...),
  [22] = array(id=>...)
)
*/

现在我不确定如何继续。我知道如何构建一个准备好创建新的多条记录的表单(Model.{n}.fieldname),但是我怎样才能创建一个允许的“编辑”表单saveAll()

尝试遍历结果数组使表单关闭..但我看不到输入元素中字段的值..

4

1 回答 1

0

你可以试试这个:

在控制器中:

public function edit() {
    $products = $this->Product->find('all');
    if (!empty($this->data)) {
        if ($this->Product->saveAll($this->data['Product'])) {
            $this->Session->setFlash('ok');
        } else {
            $this->Session->setFlash('not ok');
        }
    } else {
        $this->data['Product'] = Set::extract($products, '{n}.Product');
    }
    $this->set(compact('products'));
}

在视图中:

echo $this->Session->flash();

echo $this->Form->create('Product');

foreach ($products as $i => $product) {
    echo $this->Form->input("$i.id");
    echo $this->Form->input("$i.name");
}

echo $this->Form->end('Save');

PS:我认为您可以Product根据您的需要调整我的示例,不是吗?:)

于 2012-04-09T20:47:36.280 回答