1

I've got the model class Contactindiv with relations and search as follows.

public function relations()
{
    return array(

    'contactlogs' => array(self::HAS_MANY, 'Contactlog', 'ContactIndivID'),

    );

}

public function search()
{
    $criteria=new CDbCriteria;

    $criteria->compare('ContactIndivID',$this->ContactIndivID);
    $criteria->compare('PersonalTitle',$this->PersonalTitle,true);
    $criteria->compare('NameLast',$this->NameLast,true);
    $criteria->compare('NameMiddle',$this->NameMiddle,true);
    $criteria->compare('NameFirst',$this->NameFirst,true);

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

The current page shows the data in a searchable CGridView format.

My goal is to combine the 'contactlogs' from relations into the Model in order to have it show up on the page in a searchable fashion in the GridView. Basically add a searchable GridView column for each contact showing their contact log.

Thanks ahead of time for your help!

1 Answer 1

1

For your first goal (show contactlogs in model) you can write a getter in your main model. It depends, what you want to show in your gridview column but you could use something like:

public function getContacts()
{
    $names = array();
    foreach($this->contactlogs as $log)
        $names[] = $log->name;
    return implode(', ', $names);
}

Now you can use contacts as if it where a regular attribute of your "Contactindiv" model.

For your second goal you could add a public property which will contain the filter value, and which you can use in your search() method:

public $contactFilter;
public function search()
{
    // ...
    if(!empty($this->contactFilter)) {
        $criteria->with = array(
            'contactlogs' => array(
                'joinType' => 'INNER JOIN',
                'on' => 'contactlogs.name = :name',
             ),
        );
        $criteria->params[':name'] = $this->contactFilter;
    }
    // ..
}

Now you only need to add all the above in your gridview's columns configuration:

array(
    'name' => 'contacts',
    'filter' => CHtml::activeTextField($model, 'contactFilter'),
)

Please note, that i'm writing most of this from the top of my head and couldn't fully test it. But it should hopefully make the basic concept clear to you. Please let me know if it works.

2
  • Thanks Michael, I'll give it a go and let you know. Mar 13, 2013 at 17:26
  • Still working on it. This definitely helped me in the right direction. Thanks again. Mar 14, 2013 at 3:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.