CakePHP easy count of related entries
I have a few models: company, employee_profile, employee, employee_reviewers
a company has many employee_profiles, an employee_profile has one employee, an employee has one employee_profile, and an employee has many employee_reviewers
Given a company ID, I want to find how the total # / count of employee_reviewers. Without looping through each employee_profile/employee, is there an ea开发者_StackOverflowsy way to query for this count?
Thanks!
shouldn't it be: Company hasMany Employee, Employee hasOne Employee_Profile, Employee_Profile belongsTo Employee, Employee hasMany Employee_Reviewer?
Does each Employee_Reviewer belongsTo a Employee, or Employee_Reviewer hasMany Employee? If it's belongsTo, set up counterCache Employee_Reviewer_count in Employee model, then you can sum over that with condition Employee.company_id = $some_company_id.
If it's hasMany, you have a HABTM, it's not an easy count, because a reviewer can be associated with several employees. One way is find(list) of all the employees ids of the company, then find(list,fields=>('reviewer_id','employee_id')) on the HABTM table (I set 2 fields like that to get distinct reviewer_id values). And then just count($result)
精彩评论