CakePHP:从单个模型中检索多个记录并以一种形式进行编辑
美好的一天,
我有一个名为 ProjectRequirement 的模型,它属于 Project,因此 Project hasMany ProjectRequirements
当我创建 ProjectRequirement 条目时,我使用了此方法:
<?php
echo $this->Form->inputs(array(
'legend => false,
'fieldset' => false,
'ProjectRequirement.1.description' => ...
'ProjectRequirement.2.description' => ...
'ProjectRequirement.3.description' => ...
));
?>
我这样做是为了可以使用 saveMany() 方法来保存多个记录同时。但是,当我想在同一表单上再次编辑这些记录时,我看不到能够做到这一点。我保留了相同的字段命名结构,并尝试按如下方式设置数据:
<?php
$this->request->data = $this->ProjectRequirement->find('all', array('conditions' => ...));
?>
A pr();显示正在返回记录,但它们没有填充表单字段。如果我删除数字并只有一个像这样的字段:
<?php
echo $this->Form->inputs(array(
'legend => false,
'fieldset' => false,
'ProjectRequirement.description' => ...
));
?>
它工作正常。如何设置数据,以便在多个输入字段上设置 ProjectRequirement 中的多个记录?或者我不能?
重申一下:我在保存多条记录时没有问题,但在检索多条记录以显示时出现问题。
问候, 西蒙
Good day,
I have a model called ProjectRequirement which belongsTo Project, so Project hasMany ProjectRequirements
When I created the ProjectRequirement entries, I made use of this method:
<?php
echo $this->Form->inputs(array(
'legend => false,
'fieldset' => false,
'ProjectRequirement.1.description' => ...
'ProjectRequirement.2.description' => ...
'ProjectRequirement.3.description' => ...
));
?>
I did this so that I could make use of the saveMany() method to save multiple records at the same time. However, when I want to edit these records again on the same form, I cannot see, to be able to do it. I have kept the same field naming structure and tried to set the data as follows:
<?php
$this->request->data = $this->ProjectRequirement->find('all', array('conditions' => ...));
?>
A pr(); shows that the records are being returned, but they are not populating the form fields. If I remove the numbers and just have a single field like this:
<?php
echo $this->Form->inputs(array(
'legend => false,
'fieldset' => false,
'ProjectRequirement.description' => ...
));
?>
It works fine. How can I set the data so that multiple records from ProjectRequirement are set on multiple inout fields? Or can't I?
To reiterate: I do NOT have a problem saving multiple records, I have a problem retrieving multiple records to display.
Regards,
Simon
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您创建表单来保存许多字段时,您可以这样命名字段:
但是,在检索数据时,情况就不一样了,这里应用了数组的一般规则以及索引的工作原理。因此,只需将我的字段更改为如下所示:
有效,因为我只测试数据库中的一条记录,并且该行位于索引 0,而不是 1。
When you create the form to SAVE many fields, you name the fields like this:
However, when retrieving data, it is not the same scenario, and the general rules of arrays and how indexing works are applied here. So simply changing my fields to be like this:
Worked because I was only testing with one record in the database, and that row would have been at index 0, not 1.