0

私はyiiフレームワークの初心者です。テーブル A、テーブル B、テーブル C の 3 つのテーブルがあります。テーブル A とテーブル B は、主キーと外部キーの関係に関連付けられています。同じように、テーブル B とテーブル C は主キーと外部キーの関係にリンクされています。表示したい、フィルタ管理ページの 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',
                ),
                ),
                ),
                */

                    ));
        }
    }

2回目の課題.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,
                ));
            }
        }

3つ目: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 の 2 つのモデルからデータにアクセスしていますが、グループからはアクセスしていません。非オブジェクトのプロパティを取得しようとするとエラーが発生します

C:\wamp\www\yii\framework\base\CComponent.php(607) : eval()'d code(1) ..so plz は 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()関数でそれらにアクセスできる必要があります。私が見つけた最も簡単な方法は、BとCから表示するcol_nameごとにAにパブリック値を明示的に設定することです。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,
        ));
    }
    ...

これの利点の1つは、Col1_fromBとCol1_fromCをcgridviewの列定義の名前値として使用できるようになり、modelAの定義に入力した派手な名前を列見出しとして取得できることです。

于 2012-11-16T08:03:23.817 に答える
0

@ user181452 が言ったように、ビューを作成してからモデルを生成します。他の方法は、ターゲットモデルで、新しい各属性をモデルのパブリック変数にバインドすることです

class NewModel {
   public $new_attribute;
}

その後、ビューからアクセスできるようになります。プロジェクトにとって最もクリーンな方法を選択する必要があります。なぜなら、複雑な JOIN がある場合は、ビューを作成する方が良いからです。

于 2014-07-04T13:53:10.997 に答える
0

データベースにビューを作成し、結合操作の後に必要なすべての列を配置してから、モデルを作成します。いつもこれを使っています

于 2013-10-27T10:14:40.490 に答える
0

テーブル A からモデル C を取得するには、テーブル A と B でリレーションを定義する必要があります。
テーブル 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) には、次の 2 つの主キーが含まれている必要があります。

     * @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 に答える