Search code examples
phpmysqlgridviewyiibelongs-to

Yii CGridView BELONGS_TO relation search: Numeric comparison operator fails


I have a CGridView widget in my "PointMutationsVarscan" model set up to search a numeric non-pk value in a related model, called "GeneExpressionCufflinksGene"

I declare the search var within the PointMutationsVarscan model in the usual way to disambiguate:

public $patient_gecg_search;

Search rule in PointMutationsVarscan contains search var:

array('id, ... patient_gecg_search, geneExpressionCufflinksGene', 'safe', 'on'=>'search'),

Model relation:

'geneExpressionCufflinksGene' => array(self::BELONGS_TO,'GeneExpressionCufflinksGene',array('gene'=>'gene_id')),

Search function:

public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
// yada yada

$criteria->with[] = 'geneExpressionCufflinksGene';          
if($this->patient_gecg_search) {
$criteria->compare( 'geneExpressionCufflinksGene.fpkm', $this->patient_gecg_search, true);          
$criteria->addSearchCondition("geneExpressionCufflinksGene.fpkm",$this->patient_gecg_search);
}
return new CActiveDataProvider($this, array(
    'criteria'=>$criteria,
        'sort'=>array(
        'attributes'=>array(
            'patient_search'=>array(
                'asc'=>'patient.id',
                'desc'=>'patient.id DESC',
            ),

            'patient_gecg_search'=>array(
                'asc'=>'geneExpressionCufflinksGene.fpkm',
                'desc'=>'geneExpressionCufflinksGene.fpkm DESC',
            ),
            '*',
        ),
        ),
));
}   

In the GeneExpressionCufflinksGene model rules, fpkm is a numeric value:

array('fpkm, fpkm_conf_lo, fpkm_conf_hi', 'numerical'),

The the CGridView widget in the PointMutationsVarscan view:

$criteria=new CDbCriteria;
$dataProvider=$model->search();
$dataProvider->pagination = array('pageSize' => 15);
$columns=array();
$columns[]= array(
        'name'  => 'patient_gecg_search',
        'value'=>'$data->geneExpressionCufflinksGene->fpkm', 
        'type'  => 'raw',
        'htmlOptions'=>array('style'=>'width:250px;'),
    ); 

$this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'point-mutations-varscan',
        'dataProvider'=>$model->search(),
        'filter'=>$model,
        'columns'=>$columns,
    )
);

Essentially the problem is that I can't do a numeric comparison search (> < = etc ) on this belongs to relation column. It only does a string matching search. For example if I search for 123, it will return all the numeric fpkm values that match 123, but if I do <123 or >1 it doesn't return any records, they way you'd expect for a local parameter.

fpkm is a double precision float in the database. I can use comparison operators on fpkm just fine within the GeneExpressionCufflinksGene model's own CGridView widget. Comparison operators only fail when searching as a relation through the PointMutationsVarscan model.

Thanks for any insight you might have on this.


Solution

  • And the solution ......

    $criteria->addSearchCondition("geneExpressionCufflinksGene.fpkm",$this->patient_gecg_search, $escape=true, $operator='OR');
    

    I added the escape param and changed the operator to OR instead of default AND. My guess is it's also doing a string search in there somewhere and using the OR operator allows the comparison operation to occur in addition to the string comparison. Obviously there's something I'm not understanding but regardless, numeric comparisons now return the proper results.