php CakePHP 格式 find('all') 到 ('list') 在视图中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19920094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CakePHP format find('all') to ('list') in view?
提问by pmac89
Is there a way to format the $this->find('all') array into the $this->find('list') in the view? The reason I ask is so that I can pass that new array into the form helper options and then use the $this->find('all') array to build some additional things I need?
有没有办法在视图中将 $this->find('all') 数组格式化为 $this->find('list') ?我问的原因是我可以将新数组传递给表单助手选项,然后使用 $this->find('all') 数组来构建一些我需要的其他东西?
Array (
[0] => Array ( [School] => Array ( [id] => 0 [name] => Nature [address] => 112 Main [max_students] => 25 [application_level] => 5 ) )
[1] => Array ( [School] => Array ( [id] => 1 [name] => Math [address] => 112 Smith [max_students] => 25 [application_level] => 0 ) )
[2] => Array ( [School] => Array ( [id] => 2 [name] => Art [address] => 112 Lane [max_students] => 25 [application_level] => 0 ) )
)
So this is the array I get when I do a find('all'). I want to build the array so it looks like:
所以这是我在执行 find('all') 时得到的数组。我想构建数组,使其看起来像:
Array (
[0] => 'Nature'
[1] => 'Math'
[2] => 'Art'
)
This is usually done by the $this->find('list') function. Though the reason I want the whole array is because I need to add the application_level into $this->Form->input() function. This is because I need to add the option of class with the application level attached so I show only the shows with the application level based on the previous selection.
这通常由 $this->find('list') 函数完成。虽然我想要整个数组的原因是因为我需要将 application_level 添加到 $this->Form->input() 函数中。这是因为我需要添加附加应用程序级别的类选项,因此我仅显示具有基于先前选择的应用程序级别的节目。
EDIT: Can't I just do $this->find('list', [insert parameters here?]);? I just don't understand how you set up the additional parameters?
编辑:我不能只做 $this->find('list', [insert parameters here?]); 吗?我只是不明白你是如何设置附加参数的?
回答by Robbie Averill
If your query isn't overly complicated and isn't going to return a excessive number of results, just run it twice (once for find all and once for find list).
如果您的查询不是过于复杂并且不会返回过多的结果,只需运行两次(一次用于查找全部,一次用于查找列表)。
Find all, list, first, whatever are all the same in terms of the paramaters you pass in. E.g.:
查找所有,列出,首先,就您传入的参数而言,所有相同的内容都相同。例如:
$this->Model->find('all', array(
'conditions' => array(
'field' => 500,
'status' => 'Confirmed'
),
'order' => 'id ASC'
));
... you literally replace all
with list
. In your case, probably easiest to do it twice, once for each. Like this:
...你字面上替换all
为list
. 在您的情况下,可能最容易做两次,每次一次。像这样:
$parameters = array(
'conditions' => array(
'field' => 500,
'status' => 'Confirmed'
),
'order' => 'id ASC'
);
$alldata = $this->Model->find('all', $parameters);
$listdata = $this->Model->find('list', $parameters);
Otherwise, you can loop through it and populate your own list:
否则,您可以遍历它并填充您自己的列表:
$list = array();
foreach($findall as $row) {
$id = $row['id'];
$name = $row['name'];
$list[$id] = $name;
}
$this->set('listdata', $list);
Short answer to your question is that there's no quick, easy way to select all
andlist
from the same query, but you can re use your parameters (conditions, order etc) by passing them in as predefined arrays, or populate your own list.
对您的问题的简短回答是,没有快速、简单的方法来选择all
和list
从同一个查询中选择,但是您可以通过将参数(条件、顺序等)作为预定义数组传入,或填充您自己的列表来重新使用您的参数(条件、顺序等)。
回答by Kai
An alternative answer to creating the results formatted like find('list') from results from find('all') using CakePHP's hash utility:
使用 CakePHP 的哈希实用程序从 find('all') 的结果中创建格式为 find('list') 的结果的另一种答案:
//where $data is the result of find all
App::uses('Hash', 'Utility');
$ids = Hash::format($data, array('{n}.Model.id'), '{0}'); //ids in an array.
$names = Hash::format($data, array('{n}.Model.name'), '{0}'); //names in an array
$dataAsList = array_combine($ids, $names);
回答by Cholthi Paul Ttiopic
To improve on kai's answer. The Hash class has a method called combine that can do what you're trying to do in only one line
改进凯的回答。Hash 类有一个名为 combine 的方法,它可以只在一行中完成您想要做的事情
$list = Hash::combine($data,'{n}.Model.id','{n}.Model.name');
the $list
will be a flat array like data from find('list')
;
在$list
将扁平阵列等从数据find('list')
;