Search code examples
phpsqlyiicgridview

Get CGridView to return selected values


I would like to return selected values to the CGridView (similar to IN query) using the search function that is generated by Yii.To elaborate further let me use the example below:

Here I return values of the based on the value '34455' (fk_recordid)

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

    $criteria->compare('id',$this->id,true);
    $criteria->compare('fk_recordid',$fk_recordid,true);
    $criteria->compare('babypid',$this->babypid);
    $criteria->compare('babysbn',$this->babysbn);   

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

How would I change this code to widen the fk_recordid criteria i.e return records based on several values such as '34455','47859','78956' .....


Solution

  • Instead of compare() like you have:

    $fk_recordid = '34455';
    $criteria->compare('fk_recordid',$fk_recordid,true);
    

    You could use addInCondition() like so:

    $myRecordIds = array('34455','47859','78956');
    $criteria->addInCondition('fk_recordid',$myRecordIds);
    

    I don't know how you are passing in all of these record ids from the CGridView to the search() function, but once you have them addInCondition() will work. I hope this helps!