1

I've something like this

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

but it says Unknown column 'user.department.name' I know why it says so but how will I implement it

I've following relations

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'),

In short I want to find total number user under each category who belongs to department science

tereško
  • 58,060
  • 25
  • 98
  • 150
iThink
  • 1,239
  • 3
  • 13
  • 34

1 Answers1

0

To find total number user under each category who belongs to department science, the correct definition of the 'totalCount' relation may be somehing like this:

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

*I used 'category' assuming it as the name of the attribute that contains the foreing key to the table of categories in the User table. Of course you should change it to the correct name of the attribute.

Now simply $model->totalCount will return the number you need.

But I must say I'm not 100% sure of correctness of the syntax of the above code, but it could be easily corrected with some feedback once you tried it.

  • No it says Unknown column 'name' . Note:this name is a field of table Department – iThink Dec 24 '11 at 08:46
  • what does the relation 'totalCount' represents? – Alfredo Castaneda Garcia Dec 24 '11 at 09:07
  • Perhaps your placing the line "=>array('condition'=>'name=Science')" in the wrong place. $model=SpecialCategory::model()->with(array( 'User.Department''=>array('condition'=>'name=Science'), 'totalCount', )))->findAll(); That makes sense, but I'm not sure if that is what you're trying to do. – Alfredo Castaneda Garcia Dec 24 '11 at 09:10
  • But I dont want to filter my catgories. I only want to filter totalCount. Recheck my question .I just edited my question. Gave some detailed information. – iThink Dec 24 '11 at 09:19
  • Well... I finally got what you're doing. And I guess your code is pretty far away of doing what you want. 'with' has nothing to do here. It is used to force eager loading. I'm going to rewrite my answer shortly. – Alfredo Castaneda Garcia Dec 24 '11 at 10:00
  • Its says user.department.name column not found. I've tried every possible ways – iThink Dec 24 '11 at 12:16