Hi everyone,
I am trying to establish SELF:STAT relation where my requirement is it should get all the child records from table and then do SELF:STAT on another table.
I have table classifications(id,name,parent_id), here ‘parent_id’ column belongs to same table.
Sample data for my tables are
Classifications Images
------------------------------- -----------------------------------
id | name | parent_id | id | name |classification_id|
------------------------------- -----------------------------------
1 | Framework | 0 | 1 | Yii logo | 3 |
------------------------------- -----------------------------------
2 | Branding | 0 | 2 |Yii delete | 3 |
------------------------------- -----------------------------------
3 | Yii | 1 | 3 |jquery logo| 4 |
------------------------------- -----------------------------------
4 | Jquery | 1 | 4 |jquery ui logo| 4 |
------------------------------- -----------------------------------
for getting count of images for classification_id i have following relation in classification model
class Classifications extends CActiveRecord
{
public function relations()
{
return array(
'CountChildren' => array(self::STAT, 'Classifications', 'parent_id'),
'images_count' => array(self::STAT, 'Images', 'classification_id'),
);
}
}
Classifications::model()->with('images_count')->findAll(array('select'=>'id,name','order' => 'id'));
output will be now
name | count_of_images|
----------------------------
Framework | 0 |
----------------------------
Branding | 0 |
----------------------------
Yii | 2 |
----------------------------
Jquery | 2 |
----------------------------
my required output should be like below
name | count_of_images|
----------------------------
Framework | 4 |
----------------------------
Branding | 0 |
----------------------------
Yii | 2 |
----------------------------
Jquery | 2 |
----------------------------
my required output is shown above, for ‘Framework’ value should show as 4 because ‘Framework’ has two child classifications Yii and Jquery where for each classification count_of_images is 2.
I need to get child classifications initially and then get total count of all child classifications, how can i prepare a model query so that to get my required_output.