CakePHP:从单个模型中检索多个记录并以一种形式进行编辑

发布于 2025-01-06 12:26:13 字数 1124 浏览 0 评论 0原文

美好的一天,

我有一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凶凌 2025-01-13 12:26:13

当您创建表单来保存许多字段时,您可以这样命名字段:

  • ProjectRequirement.1.name...
  • ProjectRequirement.2.name...
  • ProjectRequirement.3.name...
  • ...

但是,在检索数据时,情况就不一样了,这里应用了数组的一般规则以及索引的工作原理。因此,只需将我的字段更改为如下所示:

  • ProjectRequirement.0.name...
  • ProjectRequirement.1.name...
  • ProjectRequirement.2.name.. .
  • ...

有效,因为我只测试数据库中的一条记录,并且该行位于索引 0,而不是 1。

When you create the form to SAVE many fields, you name the fields like this:

  • ProjectRequirement.1.name...
  • ProjectRequirement.2.name...
  • ProjectRequirement.3.name...
  • ...

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:

  • ProjectRequirement.0.name...
  • ProjectRequirement.1.name...
  • ProjectRequirement.2.name...
  • ...

Worked because I was only testing with one record in the database, and that row would have been at index 0, not 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文