Yii Framework Forum: Implement Yii Default Search - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Implement Yii Default Search Rate Topic: -----

#1 User is offline   jasonban 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 11-May 12

Posted 02 September 2013 - 11:45 PM

Hello All

I want to use yii default search for the result I get. How can I do that Suppose I have get my result using following code

$user_id = yii::app()->user->id;
$is_published = 0;
$criteria = new CDbCriteria;
$criteria->select = '*';
$criteria->condition = 'user_id=:user_id AND is_published=:is_published';
$criteria->params = array(':user_id' => $user_id, ':is_published' => $is_published);
$result = WorkRequest::model()->findAll($criteria);
$this->renderPartial('job_inprogress', array('data' => $result));


But I want to use this in the following pattern

$model = new Admission('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['Admission']))
$model->attributes = $_GET['Admission'];

$this->render('admin', array(
'model' => $model,
));

for searching..what should i do?
0

#2 User is offline   BlkRaven 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 76
  • Joined: 26-January 11

Posted 04 September 2013 - 07:41 AM

Hi jasonban

In your WorkRequest model, there should be a search function. Copy and paste that function somewhere within that model and give it another name (this is in case you need the original search function for a different purpose). I'll use the name 'wipSearch' in this example. In the wipSearch function, after the $criteria->compare lines add your condition like so:

public function wipSearch()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('attribute1',$this->attribute1); 
		$criteria->compare('attribute2',$this->attribute2);
		
		$user_id = yii::app()->user->id; //ADD THIS
		$is_published = 0;//ADD THIS
		$criteria->addCondition('user_id=:user_id AND is_published=:is_published'); //ADD THIS
		$criteria->params = array(':user_id' => $user_id, ':is_published' => $is_published); //ADD THIS
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

In your WorkProgress controller, place the following code in the controller action that will render your 'job_inprogress' view.

$model = new WorkRequest('wipSearch');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['WorkRequest']))
$model->attributes = $_GET['WorkRequest'];

$this->render('job_inprogress', array(
'model' => $model,
));


The CGridView widget in your 'job_inprogress' view should look like this:

<?php $this->widget('zii.widgets.grid.CGridView',array(
	'id'=>'workprogress-grid',
	'dataProvider'=>$model->wipSearch(),
	'filter'=>$model,
	'columns'=>array(
		'workprogressattribute1', //replace these with your attribute names
		'workprogressattribute2',
		'workprogressattribute3',
		'workprogressattribute4',
		
		array(            // display a column with "view", "update" and "delete" buttons
            'class'=>'CButtonColumn',
        ),
	),
)); ?>

I've not tested this but it should give you a general idea. To understand how it all comes together, see Yii tutorial Fundamentals.
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users