0

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

订阅者.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',
                ),
                ),
                ),
                */

                    ));
        }
    }

第二个任务.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 和subscriber)访问数据,而不是从组访问数据。并且它给出了试图获取非对象属性的错误

C:\wamp\www\yii\framework\base\CComponent.php(607) : eval()'d code(1) ..so 请检查 2 和 3 模型?需要紧急帮助。谢谢

4

4 回答 4

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() 和 for 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 定义中输入的花哨名称作为列标题。

于 2012-11-16T08:03:23.817 回答
0

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

于 2013-10-27T10:14:40.490 回答
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;

您可以在此处阅读有关关系活动记录的信息。

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

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

于 2012-11-16T08:25:43.007 回答
0

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

class NewModel {
   public $new_attribute;
}

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

于 2014-07-04T13:53:10.997 回答