title: how to join 3 models to display data in a cgridview using yii
body: <p>i am newbie in yii framework. I have 3 tables table A,table B,table C. table A and table B are related with primary and foreign key relationship.same way table B and table C are linked with priamary and foreign key relationshiop..i want to display,filter data in cgridview of admin page..using a yii fra
      updated:
  i have 3 models.subscriber.php,assignment.php,groups.php 
   i want to display attributes from 3 models and display them in cgridview of subscriber.php???</p>
  
  <p>subscriber.php..........
          
  
  <pre><code>    /**
       * This is the model class for table "users_phone_numbers".
       *
       * The followings are the available columns in table 'users_phone_numbers':
       * @property integer $id
       * @property string $name
       * @property string $phone_number
       * @property integer $created_user
       * @property string $created_date
       * @property integer $modified_user
       * @property string $modified_date
       * @property integer $status
       * @property string $message_type
       * @property string $birthdate
       * @property string $email
       *
       * The followings are the available model relations:
       * @property PhoneNumberGroupAssignment[] $phoneNumberGroupAssignments
       */
      class subscriber extends CActiveRecord
      {
  
          //public $group_search;      //for searching and displaying in cgridview
          /**
           * Returns the static model of the specified AR class.
           * @param string $className active record class name.
           * @return subscriber the static model class
           */
          public static function model($className=__CLASS__)
          {
              return parent::model($className);
          }
  
          /**
           * @return string the associated database table name
           */
          public function tableName()
          {
              return 'users_phone_numbers';
          }
  
          /**
           * @return array validation rules for model attributes.
           */
          public function rules()
          {
              // NOTE: you should only define rules for those attributes that
              // will receive user inputs.
              return array(
                  array('phone_number, created_user, created_date, birthdate, email', 'required'),
                  array('created_user, modified_user, status', 'numerical', 'integerOnly'=&gt;true),
                  array('name, phone_number', 'length', 'max'=&gt;100),
                  array('message_type', 'length', 'max'=&gt;20),
                  array('email', 'length', 'max'=&gt;150),
                  array('modified_date', 'safe'),  
                  // The following rule is used by search().
                  // Please remove those attributes that should not be searched.
                  array('id, name, phone_number, created_user, created_date, modified_user, modified_date, status, message_type, birthdate, email ', 'safe', 'on'=&gt;'search'),//add above defined variable.
              );
          }
  
          /**
           * @return array relational rules.
           */
          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(
                  'phoneNumberGroupAssignments' =&gt; array(self::HAS_MANY, 'Assignment', 'phone_number_id'),
                  //'postCount' =&gt; array(self::STAT, 'assignment', 'phone_number_id'),
              );
          }
  
          /**
           * @return array customized attribute labels (name=&gt;label)
           */
          public function attributeLabels()
          {
              return array(
                  'id' =&gt; 'ID',
                  'name' =&gt; 'Name',
                  'phone_number' =&gt; 'Phone Number',
                  'created_user' =&gt; 'Created User',
                  'created_date' =&gt; 'Created Date',
                  'modified_user' =&gt; 'Modified User',
                  'modified_date' =&gt; 'Modified Date',
                  'status' =&gt; 'Status',
                  'message_type' =&gt; 'Message Type',
                  'birthdate' =&gt; 'Birthdate',
                  'email' =&gt; 'Email',
              );
          }
  
          /**
           * Retrieves a list of models based on the current search/filter conditions.
           * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
           */
          public function search()
          {
              // Warning: Please modify the following code to remove attributes that
              // should not be searched.
  
              $criteria=new CDbCriteria;
              //$criteria-&gt;with = array( 'phoneNumberGroupAssignments' );   //new
  
  
              $criteria-&gt;compare('id',$this-&gt;id);
              $criteria-&gt;compare('name',$this-&gt;name,true);
              $criteria-&gt;compare('phone_number',$this-&gt;phone_number,true);
              $criteria-&gt;compare('created_user',$this-&gt;created_user);
              $criteria-&gt;compare('created_date',$this-&gt;created_date,true);
              $criteria-&gt;compare('modified_user',$this-&gt;modified_user);
              $criteria-&gt;compare('modified_date',$this-&gt;modified_date,true);
              $criteria-&gt;compare('status',$this-&gt;status);
              $criteria-&gt;compare('message_type',$this-&gt;message_type,true);
              $criteria-&gt;compare('birthdate',$this-&gt;birthdate,true);
              $criteria-&gt;compare('email',$this-&gt;email,true);
              //$criteria-&gt;compare( 'phoneNumberGroupAssignments.group_id', $this-&gt;group_search, true );
  
              return new CActiveDataProvider($this, array('criteria'=&gt;$criteria,
              /*'sort'=&gt;array(
              'attributes'=&gt;array(
                  'group_search'=&gt;array(
                      'asc'=&gt;'phoneNumberGroupAssignments.group_id',
                      'desc'=&gt;'phoneNumberGroupAssignments.group_id DESC',
                  ),
                  ),
                  ),
                  */
  
                      ));
          }
      }
  </code></pre>
  
  <p>2nd assignment.php
              
  
  <pre><code>        /**
           * This is the model class for table "phone_number_group_assignment".
           *
           * The followings are the available columns in table 'phone_number_group_assignment':
           * @property integer $id
           * @property integer $phone_number_id
           * @property integer $group_id
           * @property integer $created_user
           * @property string $created_date
           * @property integer $modified_user
           * @property string $modified_date
           *
           * The followings are the available model relations:
           * @property UsersPhoneNumbers $phoneNumber
           */
          class assignment extends CActiveRecord
          {
              /**
               * Returns the static model of the specified AR class.
               * @param string $className active record class name.
               * @return assignment the static model class
               */
              public static function model($className=__CLASS__)
              {
                  return parent::model($className);
              }
  
              /**
               * @return string the associated database table name
               */
              public function tableName()
              {
                  return 'phone_number_group_assignment';
              }
  
              /**
               * @return array validation rules for model attributes.
               */
              public function rules()
              {
                  // NOTE: you should only define rules for those attributes that
                  // will receive user inputs.
                  return array(
                      array('phone_number_id, group_id, created_date', 'required'),
                      array('phone_number_id, group_id, created_user, modified_user', 'numerical', 'integerOnly'=&gt;true),
                      array('modified_date', 'safe'),
                      // The following rule is used by search().
                      // Please remove those attributes that should not be searched.
                      array('id, phone_number_id, group_id, created_user, created_date, modified_user, modified_date', 'safe', 'on'=&gt;'search'),
                  );
              }
  
              /**
               * @return array relational rules.
               */
              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(
                      'phoneNumber' =&gt; array(self::BELONGS_TO, 'Subscriber', 'phone_number_id'),
                  );
              }
  
              /**
               * @return array customized attribute labels (name=&gt;label)
               */
              public function attributeLabels()
              {
                  return array(
                      'id' =&gt; 'ID',
                      'phone_number_id' =&gt; 'Phone Number',
                      'group_id' =&gt; 'Group',
                      'created_user' =&gt; 'Created User',
                      'created_date' =&gt; 'Created Date',
                      'modified_user' =&gt; 'Modified User',
                      'modified_date' =&gt; 'Modified Date',
                  );
              }
  
              /**
               * Retrieves a list of models based on the current search/filter conditions.
               * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
               */
              public function search()
              {
                  // Warning: Please modify the following code to remove attributes that
                  // should not be searched.
  
                  $criteria=new CDbCriteria;
  
                  $criteria-&gt;compare('id',$this-&gt;id);
                  $criteria-&gt;compare('phone_number_id',$this-&gt;phone_number_id);
                  $criteria-&gt;compare('group_id',$this-&gt;group_id);
                  $criteria-&gt;compare('created_user',$this-&gt;created_user);
                  $criteria-&gt;compare('created_date',$this-&gt;created_date,true);
                  $criteria-&gt;compare('modified_user',$this-&gt;modified_user);
                  $criteria-&gt;compare('modified_date',$this-&gt;modified_date,true);
  
                  return new CActiveDataProvider($this, array(
                      'criteria'=&gt;$criteria,
                  ));
              }
          }
  </code></pre>
  
  <p>3rd one :
  groups.php
            
  
  <pre><code>      /**
         * This is the model class for table "client_groups".
         *
         * The followings are the available columns in table 'client_groups':
         * @property integer $id
         * @property integer $client_id
         * @property string $title
         * @property string $keywords
         * @property integer $status
         * @property integer $created_user
         * @property string $created_date
         * @property integer $modified_user
         * @property string $modified_date
         * @property integer $is_default
         * @property integer $is_special_group
         * @property string $back_office_no
         * @property string $special_group_sms_text
         * @property string $text_sms_office
         * @property string $day_delay_1
         * @property string $subscription_sms_1
         * @property string $day_delay_2
         * @property string $subscription_sms_2
         * @property string $day_delay_3
         * @property string $subscription_sms_3
         * @property string $campaign_sms
         * @property string $delay_time_1
         * @property string $delay_time_2
         * @property string $delay_time_3
         * @property integer $msg_counter
         * @property integer $msg_counter_start_num
         * @property integer $add_expiry
         * @property integer $add_days
         * @property integer $phone_number_id
         * @property integer $ar_type
         * @property string $email
         *
         * The followings are the available model relations:
         * @property ClientInformation $client
         */
        class Groups extends CActiveRecord
        {
            /**
             * Returns the static model of the specified AR class.
             * @param string $className active record class name.
             * @return Groups the static model class
             */
            public static function model($className=__CLASS__)
            {
                return parent::model($className);
            }
  
            /**
             * @return string the associated database table name
             */
            public function tableName()
            {
                return 'client_groups';
            }
  
            /**
             * @return array validation rules for model attributes.
             */
            public function rules()
            {
                // NOTE: you should only define rules for those attributes that
                // will receive user inputs.
                return array(
                    array('client_id, created_user, created_date, msg_counter, add_expiry, add_days, phone_number_id, ar_type, email', 'required'),
                    array('client_id, status, created_user, modified_user, is_default, is_special_group, msg_counter, msg_counter_start_num, add_expiry, add_days, phone_number_id, ar_type', 'numerical', 'integerOnly'=&gt;true),
                    array('title', 'length', 'max'=&gt;250),
                    array('keywords', 'length', 'max'=&gt;255),
                    array('back_office_no', 'length', 'max'=&gt;50),
                    array('day_delay_1, day_delay_2, day_delay_3', 'length', 'max'=&gt;3),
                    array('subscription_sms_1, subscription_sms_2, subscription_sms_3, campaign_sms, email', 'length', 'max'=&gt;200),
                    array('modified_date, special_group_sms_text, text_sms_office, delay_time_1, delay_time_2, delay_time_3', 'safe'),
                    // The following rule is used by search().
                    // Please remove those attributes that should not be searched.
                    array('id, client_id, title, keywords, status, created_user, created_date, modified_user, modified_date, is_default, is_special_group, back_office_no, special_group_sms_text, text_sms_office, day_delay_1, subscription_sms_1, day_delay_2, subscription_sms_2, day_delay_3, subscription_sms_3, campaign_sms, delay_time_1, delay_time_2, delay_time_3, msg_counter, msg_counter_start_num, add_expiry, add_days, phone_number_id, ar_type, email', 'safe', 'on'=&gt;'search'),
                );
            }
  
            /**
             * @return array relational rules.
             */
            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(
                    'client' =&gt; array(self::BELONGS_TO, 'ClientInformation', 'client_id'),
                );
            }
  
            /**
             * @return array customized attribute labels (name=&gt;label)
             */
            public function attributeLabels()
            {
                return array(
                    'id' =&gt; 'ID',
                    'client_id' =&gt; 'Client',
                    'title' =&gt; 'Title',
                    'keywords' =&gt; 'Keywords',
                    'status' =&gt; 'Status',
                    'created_user' =&gt; 'Created User',
                    'created_date' =&gt; 'Created Date',
                    'modified_user' =&gt; 'Modified User',
                    'modified_date' =&gt; 'Modified Date',
                    'is_default' =&gt; 'Is Default',
                    'is_special_group' =&gt; 'Is Special Group',
                    'back_office_no' =&gt; 'Back Office No',
                    'special_group_sms_text' =&gt; 'Special Group Sms Text',
                    'text_sms_office' =&gt; 'Text Sms Office',
                    'day_delay_1' =&gt; 'Day Delay 1',
                    'subscription_sms_1' =&gt; 'Subscription Sms 1',
                    'day_delay_2' =&gt; 'Day Delay 2',
                    'subscription_sms_2' =&gt; 'Subscription Sms 2',
                    'day_delay_3' =&gt; 'Day Delay 3',
                    'subscription_sms_3' =&gt; 'Subscription Sms 3',
                    'campaign_sms' =&gt; 'Campaign Sms',
                    'delay_time_1' =&gt; 'Delay Time 1',
                    'delay_time_2' =&gt; 'Delay Time 2',
                    'delay_time_3' =&gt; 'Delay Time 3',
                    'msg_counter' =&gt; 'Msg Counter',
                    'msg_counter_start_num' =&gt; 'Msg Counter Start Num',
                    'add_expiry' =&gt; 'Add Expiry',
                    'add_days' =&gt; 'Add Days',
                    'phone_number_id' =&gt; 'Phone Number',
                    'ar_type' =&gt; 'Ar Type',
                    'email' =&gt; 'Email',
                );
            }
  
            /**
             * Retrieves a list of models based on the current search/filter conditions.
             * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
             */
            public function search()
            {
                // Warning: Please modify the following code to remove attributes that
                // should not be searched.
  
                $criteria=new CDbCriteria;
  
                $criteria-&gt;compare('id',$this-&gt;id);
                $criteria-&gt;compare('client_id',$this-&gt;client_id);
                $criteria-&gt;compare('title',$this-&gt;title,true);
                $criteria-&gt;compare('keywords',$this-&gt;keywords,true);
                $criteria-&gt;compare('status',$this-&gt;status);
                $criteria-&gt;compare('created_user',$this-&gt;created_user);
                $criteria-&gt;compare('created_date',$this-&gt;created_date,true);
                $criteria-&gt;compare('modified_user',$this-&gt;modified_user);
                $criteria-&gt;compare('modified_date',$this-&gt;modified_date,true);
                $criteria-&gt;compare('is_default',$this-&gt;is_default);
                $criteria-&gt;compare('is_special_group',$this-&gt;is_special_group);
                $criteria-&gt;compare('back_office_no',$this-&gt;back_office_no,true);
                $criteria-&gt;compare('special_group_sms_text',$this-&gt;special_group_sms_text,true);
                $criteria-&gt;compare('text_sms_office',$this-&gt;text_sms_office,true);
                $criteria-&gt;compare('day_delay_1',$this-&gt;day_delay_1,true);
                $criteria-&gt;compare('subscription_sms_1',$this-&gt;subscription_sms_1,true);
                $criteria-&gt;compare('day_delay_2',$this-&gt;day_delay_2,true);
                $criteria-&gt;compare('subscription_sms_2',$this-&gt;subscription_sms_2,true);
                $criteria-&gt;compare('day_delay_3',$this-&gt;day_delay_3,true);
                $criteria-&gt;compare('subscription_sms_3',$this-&gt;subscription_sms_3,true);
                $criteria-&gt;compare('campaign_sms',$this-&gt;campaign_sms,true);
                $criteria-&gt;compare('delay_time_1',$this-&gt;delay_time_1,true);
                $criteria-&gt;compare('delay_time_2',$this-&gt;delay_time_2,true);
                $criteria-&gt;compare('delay_time_3',$this-&gt;delay_time_3,true);
                $criteria-&gt;compare('msg_counter',$this-&gt;msg_counter);
                $criteria-&gt;compare('msg_counter_start_num',$this-&gt;msg_counter_start_num);
                $criteria-&gt;compare('add_expiry',$this-&gt;add_expiry);
                $criteria-&gt;compare('add_days',$this-&gt;add_days);
                $criteria-&gt;compare('phone_number_id',$this-&gt;phone_number_id);
                $criteria-&gt;compare('ar_type',$this-&gt;ar_type);
                $criteria-&gt;compare('email',$this-&gt;email,true);
  
                return new CActiveDataProvider($this, array(
                    'criteria'=&gt;$criteria,
                ));
            }
        }
  </code></pre>
  
  <p>current cgrid view is accessing data from two models,assignemnt and subscriber but not from groups. and it givers error rying to get property of non-object</p>
  
  <p>C:\wamp\www\yii\framework\base\CComponent.php(607) : eval()'d code(1)
  ..so plz check this for 2 and then for 3 models? urgent help needed.thanks</p>