Cgridview And Sql Count Query Column

Hi everyone,

I have the following 2 tables:




Table Order

idOrder | idUser | orderComment | ...

1       | 1      | bla          | ...

2       | 1      | bdha         | ...

3       | 2      | sdsd         | ...

4       | 4      | dsa          | ...






Table User

idUser | name 

1      | Mike 

1      | joe  

2      | ger  

4      | test 



I want to make CGridView with the following columns:

  • idUser

  • name

  • NumberOrders (should also be sortable)

How do I implement this??

I have no idea how to create the row "NumberOrders" (should also be sortable)

Currently my "search Model" for the CGridView looks as follows:




    public function search() {

        // Warning: Please modify the following code to remove attributes that

        // should not be searched.


        $criteria = new CDbCriteria;

        $criteria->condition = "role LIKE ':role'";

        $criteria->params = array(':role' => regular");


        return new CActiveDataProvider(user::model(), array(

                    'criteria' => $criteria,

                ));

    }



You should create a STAT relation in the user model and define custom attributes in the CSort object passed to dataProvider in the search method.

Hi Andreas,

Please take a look at this:

http://www.yiiframework.com/wiki/319/searching-and-sorting-by-count-of-related-items-in-cgridview/