CakePHP:如何使用Form帮助程序同时更新多个记录 [英] CakePHP: How to update multiple records at the same time with the Form helper

查看:133
本文介绍了CakePHP:如何使用Form帮助程序同时更新多个记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在模型测试的编辑页面上,我希望能够更新来自相同表单的所有关联(由hasMany)问题的Questions.order字段。



我已准备好关于saveMany的蛋糕书章()/ saveAll(),我使用 Model.0.field语法但我不知道如何告诉CakePHP 记录对应于 输入。是否 Model。#。field 对应于问题的id字段?这是我目前正在做的:

  echo $ this-> Form-> create('Question',array 'action'=>'order')); 

$ n = 0;
foreach($ questions_array as $ question):?>
<?php echo $ this-> Form-> input('Question。'。$ n。'。order'); ?>
<?php echo $ this-> Form-> input('Question。'。$ n。'。id',array('type'=>'hidden','value'=> ; $ question ['Question'] ['id'])); ?>
< input type =submitvalue =Save/>
...
$ n ++;
endforeach;
$ this-> Question-> Form-> end();

表单提交并显示为保存,但更新的订单值不对应于正确的问题记录。我做错了什么?



更新



c $ c> order action in my controller:

  public function admin_order b $ data = $ this-> request-> data; 
$ this-> Question-> saveAll($ data ['Question']);
$ this-> Session-> setFlash(Order saved。);
$ this-> redirect($ this-> referer());
}


解决方案

CakePHP将所有字段相同的索引成为数据库中的单个记录。 'index'(即 0 Foo.0.id 中)不会



例如:



<$ p

与记录的'id'有任何关系$ p> Foo.0.id = 123
Foo.0.name ='hello';
Foo.1.id = 124
Foo.1.name ='world';

如我在开始回答时提到的,索引本身并不重要, em>完全一样:

  Foo.12345.id = 123 
Foo.12345.name ='hello';
Foo.54321.id = 124
Foo.54321.name ='world';

只要相同记录的字段具有



使用


提交此数据并保存时

  $ this-> Foo-> saveMany($ this-> data ['Foo']); //只是$ this->数据可能工作得很好

CakePHP通过 Foo 模型;



表'foos';

  id name 
------------------------
123 hello
124 world

您的代码似乎使用每行可能不是正确的ID来更新这些记录

code> $ qset ['Qset'] ['id']

On an edit page for the model Test I want to be able to update the "Questions.order" field on all of it's associated (by hasMany) questions from the same form.

I've ready the Cake book chapter on saveMany()/saveAll() in the book, and I'm using the Model.0.field syntax but I can't figure out how to tell CakePHP which record to corresponds to which input. Should the # in Model.#.field correspond to the question's id field? Here's what I'm currently doing:

 echo $this->Form->create( 'Question', array('action'=>'order'));

$n = 0;
foreach ($questions_array as $question) : ?>
        <?php echo $this->Form->input('Question.'.$n.'.order' ); ?>
        <?php echo $this->Form->input('Question.'.$n.'.id', array('type'=>'hidden', 'value'=>$question['Question']['id']) ); ?>
        <input type="submit" value="Save" />
...
$n++;
endforeach;
$this->Question->Form->end();

The form submits and appears to save, but the updated order values do not correspond to the right question records. What am I doing wrong?

Update:

Here is the order action in my controller:

public function admin_order() {
    $data = $this->request->data;
    $this->Question->saveAll($data['Question']);
    $this->Session->setFlash( "Order saved.");
    $this->redirect( $this->referer() );
}

解决方案

CakePHP associates all fields with the same 'index' to be a single 'record' in your database. The 'index' (i.e. the 0 in Foo.0.id) does not have any relation to the 'id' of the record, it's just a number.

For example;

Foo.0.id   = 123
Foo.0.name = 'hello';
Foo.1.id   = 124
Foo.1.name = 'world';

As mentioned in the start of my answer, the index itself does not matter, this code will do exactly the same:

Foo.12345.id   = 123
Foo.12345.name = 'hello';
Foo.54321.id   = 124
Foo.54321.name = 'world';

As long as fields of the same record have the same 'index', CakePHP will understand that they belong 'together'.

When submitting this data and saving it using;

$this->Foo->saveMany($this->data['Foo']); // Just $this->data will probably work as well

CakePHP update two rows via the Foo model;

table 'foos';

id     name
------------------------
123    hello
124    world

Your code seems to use the same 'id' ($qset['Qset']['id']) for each row, which is probably not the right ID to update those records

这篇关于CakePHP:如何使用Form帮助程序同时更新多个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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