以下算法将用户添加到公司阵列。我想为任何给定的公司添加总共用户。我该怎么做呢
public function all_company_information($id = null){
$arrCompany = array();
$arrCompany['company']= array();
$arrData = array();
$this->autoRender = false;
$this->loadModel("User");
$users = $this->User->find('all');
foreach($users as $k => $user){
if(!in_array($user['User']['company_id'], $arrCompany['company'])){
$arrCompany['company'][$user['User']['company_id']][] = $user;
}else{
$arrCompany['company'][$user['User']['company_id']][] = $user;
}
}
}
答案 0 :(得分:1)
在公司模型上查找,然后您可以轻松获得用户数。
$results = $this->Company->find('all');
foreach($results as $k => $company) {
$results[$k]['Company']['user_count'] = count($company['User']);
}
这些方面的东西。返回的结果将是Company
个记录的数组,每个Company
记录将在User
键中包含相关用户记录的列表。