如何使用yii连接3个模型以在cgridview中显示数据

时间:2012-11-16 05:51:17

标签: yii

我是yii框架中的新手。我有3个表A,表B,表C.表A和表B都与主键和外键有关。方式表B和表C是用priamary和外键关系连接的。我想要显示,过滤管理页面的cgridview中的数据..使用yii fra     更新: 我有3个models.subscriber.php,assignment.php,groups.php  我想显示3个模型的属性,并在subscriber.php ???

的cgridview中显示它们

subscriber.php ..........         

    /**
     * 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'=>true),
                array('name, phone_number', 'length', 'max'=>100),
                array('message_type', 'length', 'max'=>20),
                array('email', 'length', 'max'=>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'=>'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' => array(self::HAS_MANY, 'Assignment', 'phone_number_id'),
                //'postCount' => array(self::STAT, 'assignment', 'phone_number_id'),
            );
        }

        /**
         * @return array customized attribute labels (name=>label)
         */
        public function attributeLabels()
        {
            return array(
                'id' => 'ID',
                'name' => 'Name',
                'phone_number' => 'Phone Number',
                'created_user' => 'Created User',
                'created_date' => 'Created Date',
                'modified_user' => 'Modified User',
                'modified_date' => 'Modified Date',
                'status' => 'Status',
                'message_type' => 'Message Type',
                'birthdate' => 'Birthdate',
                'email' => '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->with = array( 'phoneNumberGroupAssignments' );   //new


            $criteria->compare('id',$this->id);
            $criteria->compare('name',$this->name,true);
            $criteria->compare('phone_number',$this->phone_number,true);
            $criteria->compare('created_user',$this->created_user);
            $criteria->compare('created_date',$this->created_date,true);
            $criteria->compare('modified_user',$this->modified_user);
            $criteria->compare('modified_date',$this->modified_date,true);
            $criteria->compare('status',$this->status);
            $criteria->compare('message_type',$this->message_type,true);
            $criteria->compare('birthdate',$this->birthdate,true);
            $criteria->compare('email',$this->email,true);
            //$criteria->compare( 'phoneNumberGroupAssignments.group_id', $this->group_search, true );

            return new CActiveDataProvider($this, array('criteria'=>$criteria,
            /*'sort'=>array(
            'attributes'=>array(
                'group_search'=>array(
                    'asc'=>'phoneNumberGroupAssignments.group_id',
                    'desc'=>'phoneNumberGroupAssignments.group_id DESC',
                ),
                ),
                ),
                */

                    ));
        }
    }

2nd assignment.php             

        /**
         * 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'=>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'=>'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' => array(self::BELONGS_TO, 'Subscriber', 'phone_number_id'),
                );
            }

            /**
             * @return array customized attribute labels (name=>label)
             */
            public function attributeLabels()
            {
                return array(
                    'id' => 'ID',
                    'phone_number_id' => 'Phone Number',
                    'group_id' => 'Group',
                    'created_user' => 'Created User',
                    'created_date' => 'Created Date',
                    'modified_user' => 'Modified User',
                    'modified_date' => '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->compare('id',$this->id);
                $criteria->compare('phone_number_id',$this->phone_number_id);
                $criteria->compare('group_id',$this->group_id);
                $criteria->compare('created_user',$this->created_user);
                $criteria->compare('created_date',$this->created_date,true);
                $criteria->compare('modified_user',$this->modified_user);
                $criteria->compare('modified_date',$this->modified_date,true);

                return new CActiveDataProvider($this, array(
                    'criteria'=>$criteria,
                ));
            }
        }
第三个: groups.php           

      /**
       * 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'=>true),
                  array('title', 'length', 'max'=>250),
                  array('keywords', 'length', 'max'=>255),
                  array('back_office_no', 'length', 'max'=>50),
                  array('day_delay_1, day_delay_2, day_delay_3', 'length', 'max'=>3),
                  array('subscription_sms_1, subscription_sms_2, subscription_sms_3, campaign_sms, email', 'length', 'max'=>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'=>'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' => array(self::BELONGS_TO, 'ClientInformation', 'client_id'),
              );
          }

          /**
           * @return array customized attribute labels (name=>label)
           */
          public function attributeLabels()
          {
              return array(
                  'id' => 'ID',
                  'client_id' => 'Client',
                  'title' => 'Title',
                  'keywords' => 'Keywords',
                  'status' => 'Status',
                  'created_user' => 'Created User',
                  'created_date' => 'Created Date',
                  'modified_user' => 'Modified User',
                  'modified_date' => 'Modified Date',
                  'is_default' => 'Is Default',
                  'is_special_group' => 'Is Special Group',
                  'back_office_no' => 'Back Office No',
                  'special_group_sms_text' => 'Special Group Sms Text',
                  'text_sms_office' => 'Text Sms Office',
                  'day_delay_1' => 'Day Delay 1',
                  'subscription_sms_1' => 'Subscription Sms 1',
                  'day_delay_2' => 'Day Delay 2',
                  'subscription_sms_2' => 'Subscription Sms 2',
                  'day_delay_3' => 'Day Delay 3',
                  'subscription_sms_3' => 'Subscription Sms 3',
                  'campaign_sms' => 'Campaign Sms',
                  'delay_time_1' => 'Delay Time 1',
                  'delay_time_2' => 'Delay Time 2',
                  'delay_time_3' => 'Delay Time 3',
                  'msg_counter' => 'Msg Counter',
                  'msg_counter_start_num' => 'Msg Counter Start Num',
                  'add_expiry' => 'Add Expiry',
                  'add_days' => 'Add Days',
                  'phone_number_id' => 'Phone Number',
                  'ar_type' => 'Ar Type',
                  'email' => '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->compare('id',$this->id);
              $criteria->compare('client_id',$this->client_id);
              $criteria->compare('title',$this->title,true);
              $criteria->compare('keywords',$this->keywords,true);
              $criteria->compare('status',$this->status);
              $criteria->compare('created_user',$this->created_user);
              $criteria->compare('created_date',$this->created_date,true);
              $criteria->compare('modified_user',$this->modified_user);
              $criteria->compare('modified_date',$this->modified_date,true);
              $criteria->compare('is_default',$this->is_default);
              $criteria->compare('is_special_group',$this->is_special_group);
              $criteria->compare('back_office_no',$this->back_office_no,true);
              $criteria->compare('special_group_sms_text',$this->special_group_sms_text,true);
              $criteria->compare('text_sms_office',$this->text_sms_office,true);
              $criteria->compare('day_delay_1',$this->day_delay_1,true);
              $criteria->compare('subscription_sms_1',$this->subscription_sms_1,true);
              $criteria->compare('day_delay_2',$this->day_delay_2,true);
              $criteria->compare('subscription_sms_2',$this->subscription_sms_2,true);
              $criteria->compare('day_delay_3',$this->day_delay_3,true);
              $criteria->compare('subscription_sms_3',$this->subscription_sms_3,true);
              $criteria->compare('campaign_sms',$this->campaign_sms,true);
              $criteria->compare('delay_time_1',$this->delay_time_1,true);
              $criteria->compare('delay_time_2',$this->delay_time_2,true);
              $criteria->compare('delay_time_3',$this->delay_time_3,true);
              $criteria->compare('msg_counter',$this->msg_counter);
              $criteria->compare('msg_counter_start_num',$this->msg_counter_start_num);
              $criteria->compare('add_expiry',$this->add_expiry);
              $criteria->compare('add_days',$this->add_days);
              $criteria->compare('phone_number_id',$this->phone_number_id);
              $criteria->compare('ar_type',$this->ar_type);
              $criteria->compare('email',$this->email,true);

              return new CActiveDataProvider($this, array(
                  'criteria'=>$criteria,
              ));
          }
      }

当前的cgrid视图正在访问来自两个模型(assignemnt和订阅者)但不来自组的数据。并且它提供错误以获取非对象的属性

C:\ wamp \ www \ yii \ framework \ base \ CComponent.php(607):eval()'d code(1) ..那么plz检查这个2然后3个型号?需要紧急帮助。谢谢

4 个答案:

答案 0 :(得分:2)

从表A的管理视图中,您应该能够通过在列定义中列出这些值来访问其他值:

'columns'=>array(
    array('name'=>'column heading 1','value'=>'$data->modelB_nameinA->col_name1'),
    array('name'=>'column heading 2','value'=>'$data->modelB_nameinA->C->col_name2'),

要对这些值进行过滤,您需要能够在modelA-> search()函数中访问它们。我发现这样做的最简单方法是在A中为要从B和C显示的每个col_name显式设置一个公共值。您还需要将它们包含在modelA-> rules()中的安全搜索语句中,并且为简单起见,也在modelA-> attributeLabels()中。您可能还必须在A的关系中包含C,因此modelA类看起来像这样:

    class A extends CActiveRecord
{

    /**
     * @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(
            ...
            // Please remove those attributes that should not be searched.
            array('Col1_fromA, Col2_fromA, Col1_fromB, Col1_fromC', 'safe', 'on'=>'search'),
        );
    }

    /**
    * Put here the names of the columns from B and C that you are wanting to filter on
    */
    public $Col1_fromB;
    public $Col1_fromC;

    /**
     * @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(
            'modelB_nameinA'   => array(self::HAS_MANY,   'B',    'B_id'),
            'modelC_nameinA'    => array(self::HAS_MANY, 'C', array('C_id'=>'id'),'through'=>'B'),
        );
    }


    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'Col1_fromA' => 'Fancy name for Col1_fromA',
            'Col2_fromA' => 'Fancy name for Col2_fromA',
            'Col1_fromB' => 'Fancy name for Col1_fromB',
            'Col1_fromC' => 'Fancy name for Col1_fromC',
        );
    }

    /**
     * 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;
        // this is so those two tables are included in the SQL generated:
        $criteria->with = array( 'B', 'C' ); 
        $criteria->together=true;

        $criteria->compare('Col1_fromA',$this->Col1_fromA,true);
        $criteria->compare('Col2_fromA',$this->Col2_fromA,true);
        $criteria->compare('modelB_nameinA.Col1', $this->Col1_fromB,true);
        $criteria->compare('modelC_nameinA.Col1', $this->Col1_fromC,true);


        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }
    ...

这样做的一个优点是您现在可以使用Col1_fromB和Col1_fromC作为cgridview中列定义中的名称值,您将获得在modelA定义中输入的花哨名称作为列标题。

答案 1 :(得分:0)

您需要在表A和B中定义关系以从表A中获取模型C. 表A关系:

public function relations() {
    return array(
        'tableB'=>array(self::HAS_ONE, 'TableB', 'tablea_id'),
    );
}

表B关系:

public function relations() {
    return array(
        'tableC'=>array(self::HAS_ONE, 'TableC', 'tableb_id'),
    );
}

因此,您可以访问与tableA相关的tableB和tableC模型:

$modelB = $tableA->tableB;
$modelC = $tableA->tableB->tableC;

您可以阅读有关关系活动记录的here

在您的控制器操作中获取模型:

public function actionGrid() {
    $model = new ModelA('search');
    $model->unsetAttributes();          
    $this->render('view', array('model'=>$model));
}

使用CGridView查看代码:

$this->widget('zii.widgets.grid.CGridView', array(
    'id' => 'grid',
    'dataProvider'=>$model->search(),
    'filter' => $model,
    'ajaxUpdate'=>false,
    'selectableRows'=>'10',
    'columns'=>array(
            array(
                    'id'=>'checkBoxId',
                    'class'=>'CCheckBoxColumn',
            ),
            array(
                'class'=>'CDataColumn',
                'name'=>'modelA_attrib1',
                'value'=>'$data->attrib1',
                'sortable'=>true,
                'filter'=>true,
            ),
            array(
                'class'=>'CDataColumn',
                'name'=>'modelB_attrib1',
                'value'=>'$data->modelB->attrib1',
                'sortable'=>true,
                'filter'=>true,
            ),
            array(
                'class'=>'CDataColumn',
                'name'=>'modelC_attrib1',
                'value'=>'$data->modelB->modelC->attrib1',
                'sortable'=>true,
                'filter'=>true,
            ),
 ...

<强>更新
如果我理解正确,您的users_phone_numbersclient_groups表中会有MANY_TO_MANY关系(组可能与许多电话号码相关,而号码则指向多个组)。 因此phone_number_group_assignment(通常称为phone_number_group)必须包含两个主键:

     * @property integer $phone_number_id
     * @property integer $group_id

subscriber的关系:

public function relations()
{
    return array(
        'groups' => array(self::MANY_MANY, 'Groups', 'phone_number_group_assignment(phone_number_id, group_id)'),
    );
}

groups的关系:

    public function relations()
    {
        return array(
            'phone_numbers' => array(self::MANY_MANY, 'Subscriber', 'phone_number_group_assignment(group_id, phone_number_id)'),
        );
    }

因此,您可以从订阅者访问群组:$subscriber->groups

答案 2 :(得分:0)

在数据库中创建一个视图,并在连接操作后放置所有需要的列,然后创建模型。 我总是用这个

答案 3 :(得分:0)

正如@ user181452所说,创建一个View然后生成一个模型。其他方式是在目标模型中,将每个新属性与模型中的公共变量绑定

class NewModel {
   public $new_attribute;
}

然后您就可以从视图中访问它了。你必须为你的项目选择最干净的方式,因为如果你有一个复杂的JOIN,创建一个视图会更好。