有没有办法将$ 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 ) )
)
Run Code Online (Sandbox Code Playgroud)
所以这是我查找时得到的数组('all').我想构建数组,使它看起来像:
Array (
[0] => 'Nature'
[1] => 'Math'
[2] => 'Art'
)
Run Code Online (Sandbox Code Playgroud)
这通常由$ this-> find('list')函数完成.虽然我想要整个数组的原因是因为我需要将application_level添加到$ this-> Form-> input()函数中.这是因为我需要添加附加了应用程序级别的类选项,因此我只显示基于先前选择的应用程序级别的节目.
编辑:我不能只做$ this-> find('list',[在这里插入参数?]);? 我只是不明白你如何设置附加参数?
如果您的查询不是过于复杂并且不会返回过多的结果,则只需运行两次(一次查找全部,一次查找列表).
查找所有,列表,首先,根据您传递的参数,所有相同的内容.例如:
$this->Model->find('all', array(
'conditions' => array(
'field' => 500,
'status' => 'Confirmed'
),
'order' => 'id ASC'
));
Run Code Online (Sandbox Code Playgroud)
......你真的替换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);
Run Code Online (Sandbox Code Playgroud)
否则,您可以遍历它并填充您自己的列表:
$list = array();
foreach($findall as $row) {
$id = $row['id'];
$name = $row['name'];
$list[$id] = $name;
}
$this->set('listdata', $list);
Run Code Online (Sandbox Code Playgroud)
简短的回答你的问题是,有没有快速,简单的方式来选择all ,并 list从相同的查询,但你可以通过将其作为预定义的阵列重新使用你的参数(条件,顺序等),或填充自己的列表.
使用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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
21772 次 |
| 最近记录: |