Cdbcriteria Does Not Work For Two Fields At The Same Time

I have created 4 radiobuttons in advanced search form using the below code.




    <div class="row">

    		<?php #echo $form->label($model,'ReviewedDate'); ?>		

                    <?php echo $form->radioButtonList($model, 'ReviewedDate', 

            array('1' => 'Reviewed', '' => 'Not Reviewed')

        ); ?>

    	</div>

    

           <div class="row"> 

    	            <?php echo $form->radioButtonList($model, 'Approved', 

            array('0' => 'Rejected', '1' => 'Approved')

        ); ?> 

    	</div>   

So now I have created some criteria in model for filtering the gridview to display the rows according to radiobutton value.




    if($this->ReviewedDate != '') 

                    {

                    $criteria->addCondition('ReviewedDate ' . ($this->ReviewedDate=='1' ? 'IS NOT NULL' : 'IS NULL'));

                    $criteria->addCondition('updateddate > createddate');

                    $criteria->compare('revieweddate','0000-00-00 00:00:00');

                    }

                    else 

                    {

                    $criteria->addCondition('updateddate = createddate');    

                    $criteria->addCondition('ReviewedDate IS NULL');

                    }


 


    if (isset($this->Approved) && !empty($this->Approved)) {

                            

                            $criteria->addCondition("approved='" . $this->Approved . "'");

                            $criteria->condition = ' (updateddate > createddate) AND (revieweddate IS NOT NULL) and approved = 1 ';

                    }

                    else {

                            

                            $criteria->addCondition("approved='" . $this->Approved . "'");

                            $criteria->condition = ' (updateddate > createddate) AND (revieweddate IS NOT NULL) and approved = 0 ';

                    }



The above code works fine and displays the rows accordingly if either revieweddate criteria is commented or the approved criteria is commented. But, both the criteria does not work at the same time.

When I try to use both the criteria at the same time, one of the criteria works.

  1. Now, I want to create individual radiobutton for each option (I need 4 radiobuttons approved, rejected, reviewed, not reviewed).

  2. I want the radiobutton to work for each criteria without overlapping.

How can I do this

Your criteria building code is quite complicated. Wouldn’t that be easier to start from the end, that is look at the generated SQL query? Check your logs. Ff YII_DEBUG is on and db profiling is enabled in the config you should see all executed queries.

Remember, that the default operator when calling various CDbCriteria methods is ‘AND’.

Also, in the second block for ‘Approved’ you set the value of condition property, overriding previously appended conditions.