Implementing Search On Sitecontroller For Different Models

Hi all.

I’m trying to implement a global search functionality that should search on different models depending one value passed as $_REQUEST from the search form.

My search form has one text field and a dropdown select with the ‘ambito’ to search.

My approach is to switch among different ‘ambito’ and prepare the right query with passed text value.

An example trying clarify:


$_POST[‘datum’]=‘marti’; So I will look in ‘client’ for everyone who has %marti% in its lastName_1 (or lastName_2) field

An user can search values for

  • ‘last_name’ with ‘%like%’ in client model.

  • ‘doc_number’ strict with ‘=’ in client model.

  • ‘address’ with ‘%like%’ in client model.

  • ‘plate’ strict with ‘=’ in vehicle model.

  • ‘invoice_number’ strict with ‘=’ in invoice model.

  • ‘recipe_number’ strict with ‘=’ in recipe model.

Hope it’s clear enough, but it’s just an example. Final development will be not much more than that.

So I started creating an actionSearch in site controller:

	public function actionSearch()


	// diferenciar guest y logged !!!!!!!!!

		//echo "Buscando-".$_SERVER['REQUEST_URI']."///";

		//foreach($_POST AS $k => $v) echo $k."--".$v." ..... ";

		// data passed $_POST['ambito'] & $_POST['datum']


		switch($_POST['ambito']) {

			case 'lastName':

					$ambito = 'client';

					$column = 'lastName_1';// or lastName_2 !!! LIKE %%

					// then newModel(); ???



			case 'plate':

					$ambito = 'vehicle';

					$column = 'plate';// or VIN !!!



			case 'address':

					$ambito = 'client';

					$column = 'address';	// LIKE %% !!!



			case 'invoice':

					$ambito = $_POST['ambito'];

					$column = 'invoice_number';

					$condit = 'invoice.invoice_number = "'.$_POST['datum'].'"';

					$model = new Invoice();



// 			case 'recipe':

// 					break;



					$ambito = $_POST['ambito'];	

					$column = 'NO COLUMN GIVEN'; // ERROR



//		echo $ambito."->".$column."=".$_POST['datum'];

// objective->find the id from right parent

// then make a renderpartial or redirect

// 		$model=new Invoice('search');

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

// 		$model->invoice_number=$_POST['datum'];

// 		if(isset($_GET['Invoice']))

// 			$model->attributes=$_GET['Invoice'];


// 		$this->render('admin',array(

// 			'model'=>$model,

// 			'id'=>$model->invoice_id,

// 		));

	$model = Invoice::model()->findByAttributes(array('invoice_number'=>$_POST['datum']));			

// echo "<pre>";

// print_r($model->invoice_id);

// echo "</pre>";

	if(!$model) throw new CHttpException(404,'The specified '. $ambito. ' -> '.$_POST['datum'].' cannot be found.');


	$this->redirect(array('invoice/view', 'id'=>$model->invoice_id));// trying with admin instead of view *********


Form action is ‘site/search’.

Of course code is not performing ‘correctly’. I just passed the switch for testing and searching only in invoice at the moment !!! ( $model = Invoice::model()->findByAttributes ). I’m trying to build the search query from the switch, something like:

$model = $ambito::model()->findByAttr($column => $_POST['datum'])

My objective is get the right ID from the query to get the desired result in a subsequent redirect or render, but I’m not sure if it really is the way to go.

More or less is working, but only when I try to get exactly only one result.

But if I need to get all clients based on lastName, with a ‘%like%’ query, I’m going to get more than one result, and I want to present all results in a table, so the user can choose which result he really is looking for.

I’m relatively new to Yii, so I’m thinking I’m missing something for the search functionality inside Yii, if there exists.

If anybody knows a better approach, please, let me know, and point me in the right direction.



you can use a SQL requestwith params (use createCommand for this) to insert rows in a result table, and then show a view of this table for the user.

Hey. I’m trying this for a while… If you get some orientation… let me know!