How to add a parameterized subquery as a relation/scope to an activerecord?


I have 2 models: Product and Attribute. Each Product can have many Attribute. I define a new property for Product attributeIds where i want to get all attributes’ id separated by comma (ex: if product x has 5 attributes (id from 1 to 5) the attributeIds property must be filled with the string “1,2,3,4,5”).

To solve my problem , I defined a scope but when i use it into a CGridView it throw me the error "Invalid parameter number: number of bound variables does not match number of tokens. " when he tries to execute the count. (the parameter added by me is not present into the count query)

// Product model

class Product {

   public $attributeIds;


   function getAttrIds($limit = 3) {

        $criteria = array(

                       'select' => '(select group_concat(id) from '.Attribute::model()->tableName().' where = attribute.product_id limit :limit) as attributeIds',

                       'params' => array(':limit'=>$limit),



        return $this;





// call

$this->widget('zii.widgets.grid.CGridView', array(





Does anyone know how to attach a parameterized subquery to an ActiveRecord ?


i found a solution, i added the field as a stat relation.