我正在通过CakePHP findbyList获取数据
Array
(
[92] => 5
[93] => 5
[98] => 5
)
码
$get = array(
'fields' => array('Upload.type'),
'joins' => array(
array(
'alias' => 't',
'table' => 'temp_files',
'type' => 'LEFT',
'conditions' => array('Upload.id = t.file_id')
),
array(
'alias' => 'o',
'table' => 'orders',
'type' => 'LEFT',
'conditions' => array('o.batch_id = t.batch_id')
)
),
'conditions' => array('OR'=>array('o.user_id '=>$user['id'],'t.user_id '=>$user['id'])),
'group' => 'Upload.id'
);
$files = $this->find('list',$get);
CakePHP中是否有任何方法可以获取如下数据:
Array
(
[5] => Array
(
[0] => 92
[1] => 93
[2] => 98
)
)
是的,有两种cakephp方法可以检索像你这样的结果:
查找线程:
https://book.cakephp.org/2.0/en/models/retrieving-your-data.html#find-threaded
并使用哈希实用程序来提取,组合等结果:
https://book.cakephp.org/2.0/en/core-utility-libraries/hash.html
不知道你的数据库是怎么样的我可能会使用$this->find('all',$get)
而不是'list'
List将始终将模型的ID作为键,因此它将永远不会按您的需要工作。
如果提到db结构,理解这种情况会好得多。无论如何我建议使用$ this-> find('all',$ get)而不是list,你可以通过管理你需要最终输出数组的方式来玩你的最终结果数组。
EX: - 如果您获得带有userID的用户名列表,那么您在CakePHP查询中使用CakePHP集合。 CakePHP系列在这里:CakePHP Collection
$userList = $this->find()
->where(['status' => Configure::read('Status.active')])
->order(['first_name' => 'ASC'])
->combine('id','name')
->toArray();
在此结果中,userID是一个键,名称作为值显示。