How to Search a calculated field

Hi, I have a question regarding CGridView and its Dataprovider.

I’m listing within a CGridView the result of a search() function that use $criteria->compare() and method $criteria->select().

The problem is that I need to compare a field that is calculated on the fly with $criteria->select() this way:

Inside Log.php Model

$criteria->select=array("*","if(t.description='', CONCAT('from ',from0.description,' to ',to0.description),t.description) as mydescription");


The compare doesn’t work since mydescription does not exist on the Log table.

Any suggestion?

Do you have $this->mydescription? If not make make a virtual attribute with getter and setter:

private $_mydescription;

public function getMydescription() {

 if($this->_mydescription !== null) {


    if($this->description == '') {

        $this->_mydescription = //create your concat function here

    } else {

        $this->_mydescription = $this->description;



 return $this->_mydescription;


public function setMydescription($desc) {

  $this->_mydescription = $desc;


public function search() {


$criteria->compare("if(t.description='', CONCAT('from ',from0.description,' to ',to0.description),t.description)", $this->mydescription, true); //look for no underscore



This should work.

Hey Gasmin, thank you very much!! I already had the mydescription so I don’t need the getter function, anyway this is something that you teach me and that I will surely use for other stuff. Instead you showed me the solution: put the “if(t.description…)” expression inside the compare() directly and not trying to bind a column!

Have a nice week end my friend and many thanks for the time you spent for me! :)