Filtering with data relations

I have a 3 tables:

  1. Programs (Programs Tbl)

  2. Payments (TblPayments)

  3. Table linking the two above, as the one product can have multiple payments (TblPaymentDict)



‘tblPayment’ => array (self :: MANY_MANY, ‘TblPayment’, ‘tbl_payment_dict (id_program, id_payment)’)


‘programs’ => array (self :: MANY_MANY, ‘TblPrograms’, ‘tbl_payment_dict (id_payment, id_program)’)

And I have the CGridView with program list, where each program, in the column ‘payments’ can have more than one option (why I have TblPaymentDict). It works. If a program has more than one payments method, then user can see it.

It does not work with filtering.

array (

‘name’ => ‘id_payment’

‘filter’ => $ model-> getPaymentList ()

‘value’ => ‘$ data-> getRelatedPayments ()’,


getPaymentList public function ()


$payArray = CHtml::listData(TblPayment::model()->findAll(array(‘order’ => ‘name’, “condition”=>“is_active = 1”)), ‘id’, ‘name’);

return $ payArray ;


public function getRelatedPayments() {


return implode(’, ', $out);


Don’t know what is the problem with. With wrong filtering method, relations, or mayble with search function:

$criteria=new CDbCriteria;







return new CActiveDataProvider($this, array(



Help :(

BTW, it’s better to have another table to set relations between programs and payments (TblPaymentDict) or it’s better to save multiple payment in program table (as one column) using for example json?

Is $id_payment declared as a public property of the TblPrograms model? If not, you have to declare it.

And in the "search" method of the TblPrograms model:

    $criteria = new CDbCriteria;

    $criteria->with = array(

        'tblPayment' => array(

             'select' => false,

             'together' => true,



    $criteria->group = 't.program_id';





    // $criteria->compare('id_payment',$this->tblPayment); 

    $criteria->compare('tblPayment.id_payment', $this->id_payment);


    return new CActiveDataProvider($this, array(



I’m not sure if the code above will work for you or not, but it should be something like that.

Also please take a look at the following wiki for details about ‘with’, ‘select’, ‘together’ and ‘group’ in the code above.

Yii 1.1: Drills : Search by a HAS_MANY relation in Yii 1.1

The wiki is written for HAS_MANY relation, but it’s almost the same with MANY_MANY relation.

Thank you thank you thank you!!!!!!!!!! It’s working now :)