Dynamic search variable on a model

Hi all,

I am still a newbie and am stuck on searching on a model using a dynamic variable.

I have 2 models MdGeneral(md_id,…) and MdTerm(md_id,term_code,…)

Model [color="#FF0000"]MdGeneral[/color] looks like this, which works!

public function search()


       $term = "2005-2010";

   $criteria=new CDbCriteria;

   $criteria->condition = "(

                               MPID IN(SELECT MdID FROM [color="#FF0000"]md_term[/color] WHERE term_code ='".$term."')



	return new CActiveDataProvider(get_class($this), array(




BUT I want to be able to dynamically specify $term from a textfield in my mpGeneral view, _search form.

section of _search form looks like this:

<div class="row">

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

   <?php echo $form->textField($model,'md_id',array('size'=>20,'maxlength'=>50)); ?>


<div class="row">

   <?php echo $form->label($mdTerm,'term_code'); ?>		

   <?php echo $form->textField($mdTerm,'term_code',array('size'=>20,'maxlength'=>50)); ?>


I can’t seem to $_GET [color="#FF0000"]$mdTerm->term_code [/color]from the [color="#FF0000"]mdGeneral controller[/color],

My controller looks like this,

public function actionAdmin()


	$model=new MdGeneral('search');

	$mdTerm=new MdTerm('search');

	$model->unsetAttributes();  // clear any default values



		$term = $_GET['MdTerm'];








Any help will be much appreciated. Thanks in advance.

Add this term as new property of the model.

in the model:


  class MdGeneral extends CActiveRecord


    public $MdTerm;



And add to safe attributes value for search.

Your search will be:

$criteria->addcondition =( "(

MPID IN(SELECT MdID FROM md_term WHERE term_code ='".$this->MdTerm."')


You don’t have to change your controller, and you can generate the text field as

CHtml::activeTextField($model, 'MdTerm');

Good job Zaccaria, it is exactly what I wanted, thanks a lot mate! Much appreciated your help.