Ignore or override a compare with keep other things compare in Yii1

I use Yii version 1 (Sorry for this job! :wink: )

I need to remove and ignore a compare or override it.

See this code:


    $criteria = new CDbCriteria();

    $criteria->compare('title', 'anything');

    //...more criteria in here

    $criteria->compare('userId', 100);

    $models = Video::model()->findAll($criteria);

    var_dump($models);



For above example, I need ignore userId in compare or search for another user, If there is not model for it and search for all models with keep other things compare and join tables in criteria.

Be like this code:


    $criteria = new CDbCriteria();

    $criteria->compare('title', 'anything');

    //...more criteria in here

    $criteria->compare('userId', 100);

    $models = Video::model()->findAll($criteria);

    if (!$models) {

      $criteria->ignoreCompare('userId'); //just ignore `userId` and keep other things criteria

      $criteria->overrideCompare('userId', 200); //or just override `userId` and keep other things criteria

      $models = Video::model()->findAll($criteria);

    }

    var_dump($models);



The ignoreCompare and overrideCompare itโ€™s just imaginary methods! I donโ€™t found any method be like these.

How can I do it?

Hi Nabi,

As per my understanding, If the user id (say 100) does not exist then the model should return all records?

[size=2]In this case, simply you may use partial match[/size]

[size=2]

[/size]




$criteria->compare('userId', 100, true); // Partial Match



or you can do like as follows





$criteria = new CDbCriteria();

$criteria->compare('title', 'anything');

//...more criteria in here

$criteria->compare('userId', 100);

$models = Video::model()->find($criteria);

if (!$models) {

   $criteria->compare('userId', '',true); //Will be ignored

    $models = Video::model()->findAll($criteria);

}

var_dump($models);



I hope this will help you. For more details Compare Reference