Search Form with two models

The search form I am creating will contain fields from two models - User and Member

When the form is submitted (GET method) it will contain variables from $_GET[‘User’] and $_GET[‘Member’]

Is it possible for the variables that are submitted to not include the model name? for example:

/mysite/search?gender=1&type=1

instead of:

/mysite/search?User[gender]=1&Member[type]=1

This is possible to do, you have to create the form field with CHtml::textbox instead of CHml::activeTextbox.

Another approach is to use the 2 models for create standard active input field, and then write a composite search function.

You can create the search function for user that will return a CDbCriteria and pass it to the search to the member user, for example.

This will allow you to do:




public function actionSearch()

{

  $user= new User('search');

  $Member= new User('Member');


  // set atttibutes to both


  $dataProvider= $user->combinedSearch($member->combinedSearch());


}






I see. I will probably use a normal textField / dropDownList instead of Active one.

BTW when using active fields, does it not automatically populate the field with the GET variable value?

EDIT: Yes it does. I forgot to declare some of the attributes as safe in my model.

Is there a way of easily populating non ‘Active’ fields with the GET variable values? For example currently I have to do something like this on each field:


<?php echo CHtml::dropDownList('gender', isset($_GET['gender']) ? $_GET['gender'] : null , $user->GenderOptions, array('prompt'=>'Any')); ?>

I know when you use active attributes you can just do this in the controller:


if(isset($_GET['User']))

{

	$user->attributes=$_GET['User'];

}

Anybody got any suggestions on the above?

I always use active fields.

If it is not supposed to be saved in database, I use CFormModel. This is a really good class: it gives you validation rules (and safe attributes), attribute label (with required label in views) and setAttribute.

I advice you to use the same (and I considere a bad practice to rely on $_GET in views, is really not mvc)

Can you give me example how I can use CFormModel to set the values of my GET attributes?

First you have to create a CFormModel:




class MyModel extends CFormModel

{

    public $user;

   

    public function rules...

    public function attributeLabels...

}



Then in the controller:




public functionAction...()

{

  $model= new MyModel;

  

  if (isset($_GET['MyModel']))

      $model->attributes=$_GET['MyModel'];


   ...

}



Exactly the same as you use active record. In the view you should use a normal active field.

Aaaah - that works but it brings me back to square one - the model name is included in the URL - makes it look very untidy - not that it really matters but I prefered the standard query string!

Of corse… now I remember why I always use POST method, looks really odd with this bracket in url.

As I am a lazy programmer, I always managed to use POST and CFormModel, so I cannot give you further assistance to more laborious solutions :)