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)

Relations:

programs:

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

payments:

‘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() {

$out=CHtml::listData($this->tblPayment,‘id’,‘name’);

return implode(’, ', $out);

}

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

$criteria=new CDbCriteria;

$criteria->compare(‘id’,$this->id);

$criteria->compare(‘name’,$this->name,true);

$criteria->compare(‘id_category’,$this->id_category);

$criteria->compare(‘id_subcategory’,$this->id_subcategory);

$criteria->compare(‘id_payment’,$this->tblPayment);

$criteria->compare(‘description’,$this->description,true);

return new CActiveDataProvider($this, array(

'criteria'=>$criteria,

));

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',$this->id);

    $criteria->compare('name',$this->name,true);

    $criteria->compare('id_category',$this->id_category);

    $criteria->compare('id_subcategory',$this->id_subcategory);

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

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

    $criteria->compare('description',$this->description,true);


    return new CActiveDataProvider($this, array(

	'criteria'=>$criteria,

    ));



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 :)