CDbCriteria compare...weird behavior

Hi there…

I have several models with relations, the CRUD generated by YIIC, and made my own templates to generate the views. Nothing great.

The thing is that when I access the records admin page generated (wich show the datagrid with the records), it tries by the default bind the values from the activeDropDownList from the search form…

For example, I have a model called Curso, and two relations, Materia and Profesor:




public function relations()

{

	// NOTE: you may need to adjust the relation name and the related

	// class name for the relations automatically generated below.

	return array(

		'materia' => array(self::BELONGS_TO, 'Materia', 'materia_id', 'alias' => 'materias'),

		'profesor' => array(self::BELONGS_TO, 'Profesor', 'profesor_id', 'alias' => 'profesores'),

	);

}

...

...

...

// and here the search function

public function search()

{

	// Warning: Please modify the following code to remove attributes that

	// should not be searched.


	$criteria=new CDbCriteria;


	$criteria->compare('materia_id',$this->materia_id);

	$criteria->compare('profesor_id',$this->profesor_id);


	$criteria->with = array('materia', 'profesor');

	$criteria->order = 'fecha DESC';


	return new CActiveDataProvider('Curso', array(

		'criteria'=>$criteria,

		'sort' => false,

	));

}



When I access the admin page, by default the sql generated is:




SELECT COUNT(DISTINCT `t`.`id`) FROM `cursos` `t`  

LEFT OUTER JOIN `materias` `materias` ON (`t`.`materia_id`=`materias`.`id`) 

LEFT OUTER JOIN `profesores` `profesores` ON (`t`.`profesor_id`=`profesores`.`id`)

WHERE ((materia_id=:ycp0) AND (profesor_id=:ycp1))



This is, it tries to bind the values from the search form, and I didn’t search anything, so it founds nothing…

If I DO USE the dropdowns, it works well.

And btw, this doesn’t happen with other input types (at least not with text inputs).

If you think this is weird, let my say that this ONLY happen in the production servers, that runs PHP Version 5.2.13, and not in my local server (running PHP Version 5.3.2-1ubuntu4.2).

My workaround is a bit ugly, changing the search function this way to see when I actually search something:




public function search()

{

	// Warning: Please modify the following code to remove attributes that

	// should not be searched.


	$criteria=new CDbCriteria;


	// HERE

	if(isset($_GET['Curso']['materia_id'])){

		$criteria->compare('materia_id',$this->materia_id);

	}


	// AND HERE

	if(isset($_GET['Curso']['profesor_id'])){

		$criteria->compare('profesor_id',$this->profesor_id);

	}


	$criteria->with = array('materia', 'profesor');

	$criteria->order = 'fecha DESC';


	return new CActiveDataProvider('Curso', array(

		'criteria'=>$criteria,

		'sort' => false,

	));

}



Anyone know something about this extrange behavior???

Thanks a lot!

Hi Luciano,

I’m having the same problem - http://www.yiiframework.com/forum/index.php?/topic/9961-cgridview-and-a-drop-down-list/ . It’s either an error in the way I’m using the framework, or the CDbCriteria is strange…

Thanks for the workaround,

With kind regards,

Bill

Thank you! At least we know now what is this problem about!

:)