0

以下算法将用户添加到公司数组。我想为任何给定的公司添加用户总数。我是怎么做的

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;

                }

        }

    }
4

1 回答 1

1

在 Company 模型上进行查找,然后您可以轻松获得用户数。

$results = $this->Company->find('all');
foreach($results as $k => $company) {
    $results[$k]['Company']['user_count'] = count($company['User']);
}

类似的东西。返回的结果将是一个Company记录数组,每条Company记录的User键中都有一个关联的用户记录列表。

于 2012-05-10T19:19:21.577 回答