【问题标题】:CakePHP: Retrieve multiple records from a single model to be edited in one formCakePHP:从单个模型中检索多个记录以以一种形式进行编辑
【发布时间】:2023-03-29 09:40:02
【问题描述】:

早安,

我有一个名为 ProjectRequirement 的模型,它属于 Project,所以 Project 有许多 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' => ...));
?>

pr();显示正在返回记录,但它们没有填充表单字段。如果我删除数字并只有一个这样的字段:

<?php
     echo $this->Form->inputs(array(
          'legend  => false,
          'fieldset' => false,
          'ProjectRequirement.description' => ...
     ));
?>

它工作正常。如何设置数据,以便在多个 inout 字段上设置来自 ProjectRequirement 的多个记录?还是不能?

重申:我在保存多条记录时没有问题,我在检索要显示的多条记录时遇到了问题。

问候, 西蒙

【问题讨论】:

    标签: cakephp model associations cakephp-2.0


    【解决方案1】:

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

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

    但是,在检索数据时,情况就不一样了,这里应用数组的一般规则和索引的工作方式。所以只需将我的字段更改为这样:

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

    之所以有效,是因为我只测试了数据库中的一条记录,而该行的索引应该是 0,而不是 1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多