CakePHP 在 select 输入中使用不同值的简便方法

CakePHP easy way to use different values in select input

我对 CakePHP 有所了解 "problem"。 我的代码工作正常,但我正在寻找一种方法来缩短代码。

我有一个名为 "Document" 的模型,它 "belongsTo" 有几个 "User"。

在我的控制器中,我使用以下代码加载所有可用用户的列表:

$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 中包含所有相同的元素。 总的来说,一个 "Document" 连接到 6 个不同的 "User",所以这确实使我的代码膨胀。

有什么想法吗? 谢谢!

如果它们都与相同的 table 和模型相关联,而不只是这样做:

$users = $this->Document->Creator->find('list');

在视图中需要相同列表的任何地方都使用相同的列表。