我在使用CakePHP时遇到了一点"问题"。我的代码工作正常,但我正在寻找一种缩短代码的方法。
我有一个名为"文档"的模型,它"属于"几个"用户"。
- 造物主
- 编辑 器
- 负责人
在我的控制器中,我使用以下代码加载所有可用用户的列表:
$creators= $this->Document->Creator->find('list');
$editors= $this->Document->Editor->find('list');
$responsiblepersons= $this->Document->Responsibleperson->find('list');
$this->set(compact('creators','editors','responsiblepersons');
现在我可以在我的视图中使用此代码:
echo $this->Form->input($creator_id);
echo $this->Form->input($editor_id);
echo $this->Form->input($responsibleperson_id);
这工作正常,但是真的有必要找到所有 3 个列表吗?在我的控制器$creators中,$editors和$responsiblepersons包含所有相同的元素。总的来说,一个"文档"连接到 6 个不同的"用户",所以这真的让我的代码膨胀。
知道吗?谢谢!
如果它们都与同一个表和模型相关联,而不仅仅是:
$users = $this->Document->Creator->find('list');
并在视图中需要相同列表的所有位置使用相同的列表。