Searching And Sorting From 2 Models

I have 2 tables

  1. users

  2. coupons

I have a form in my “coupons” view in which I want to search users after some filtration but I get error

Property "CDataColumn.username" is not defined.

This is in my Coupons Model class


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(

			'users' => array(self::MANY_MANY, 'Users', 'allocated_coupons(coupons_id, users_id)'),

			'zone' => array(self::BELONGS_TO, 'Zone', 'zone_id'),

			'countries' => array(self::BELONGS_TO, 'Countries', 'countries_id'),

			'invoices' => array(self::HAS_MANY, 'Invoice', 'coupons_id'),

			'pricingPlans' => array(self::MANY_MANY, 'PricingPlans', 'pricing_plans_has_coupons(coupons_id, pricing_plans_id)'),

		);

	}

And also some editing in Coupons model Search function


public function search()

	{

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

		// should not be searched.


		$criteria=new CDbCriteria;

		

		$criteria->with = array( 'users' );

		$criteria->together = true;

		$criteria->compare( 'users.username', $this->users_search, true );

		$criteria->compare( 'users.id', $this->users_search, true );

…………

Also some editing in rules() in Coupons model class


public $users_search;

public function rules()

	{

array('users_search', 'safe', 'on'=>'search'),



In my search widget in view I use this


<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'coupons-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'id',

		'issue_date',

		'start_date',

		'expiry_date',

		'count_value',

		'code',

		 array( 'username'=>'users_search', 'value'=>'$data->users->username' ),

it is because you have:


array( 'username'=>'users_search', 'value'=>'$data->users->username' ),

in your widget definition.

it should be:


array( 'name'=>'users_search', 'value'=>'$data->users->username' ),

I only need filtered users from Users model in coupons view. I use this code and Users data shown in Grid, but filter not working


$model2 = new Users('search');


$this->widget('zii.widgets.grid.CGridView', array(

'dataProvider' => $model2->search(),

'filter' => $model2,

'columns' => array(

   

                 array(

            'id' => 'selectedIds',

            'class' => 'CCheckBoxColumn',

                        'selectableRows' =>'2',

        ),

   

   'id',

   'username',

  

                

),


));