二、范围和关系问题


Yii scope and relation issue

我有这样的东西

$model=UserCategory::model()->findAll(array('with'=>array('user.department','totalCount'=>array('condition'=>"user.department.name='Science'"))));

但是它显示未知列'user。department。name'我知道它为什么这么说但是我该如何实现它

我有以下关系

UserCategory
'user' => array(self::HAS_MANY, 'UserCategory', 'categoryId'),
'totalCount' => array(self::STAT, 'UserCategory', 'categoryId'),//counts total of user under each category
User
'userCat' => array(self::BELONGS_TO, 'UserCategory', 'categoryId'),
'department' => array(self::BELONGS_TO, 'Department', 'departmentId'),
Department
'userDept' => array(self::HAS_MANY, 'User', 'departmentId'),

简而言之,我想找到每个类别下属于部门science的用户总数

要找到每个类别下属于department science的用户总数,'totalCount'关系的正确定义可能如下所示:

'totalCount' => array(self::STAT, 'User', 'category' /* *See note below */, 'condition'=>'user.department=Science'),

*我使用'category',假设它是包含User表中类别表的前键的属性名。当然,您应该将其更改为属性的正确名称。

现在只需$model->totalCount就会返回您需要的数字。

但是我必须说,我不能100%确定上面代码的语法正确性,但是一旦你尝试了,它可以很容易地通过一些反馈来纠正。