2013-07-21 59 views
1

我使用Yii MVC如何在Yii中爲相同的AR模型連接規則

我必須經常在數據表中添加新列。

我想使用Gii並重新生成model並保留我做的chnages;更改,如常量,變量,規則,關係,等等

所以,讓我們例如url類模型:

<?php 

/** 
* This is the model class for table "url". 
* 
* The followings are the` available columns in table 'url': 
* @property string $id 
* @property integer $instance_id 
* @property integer $website_id 
* @property string $link 
* @property string $title 
* @property integer $created 
* @property integer $updated 
* @property integer $status 
*/ 
class Url extends CActiveRecord { 

    const ACTIVE = 1; 

    /** 
    * Returns the static model of the specified AR class. 
    * @param string $className active record class name. 
    * @return Url 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 'url'; 
    } 

    /** 
    * @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('instance_id, website_id, link, title, created, updated, status', 'required'), 
      array('instance_id, website_id, created, updated, status', 'numerical', 'integerOnly' => true), 
      array('link, title', 'length', 'max' => 255), 
      array('link', 'unique'), 
      // The following rule is used by search(). 
      // Please remove those attributes that should not be searched. 
      array('id, instance_id, website_id, link, title, created, updated, status', '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(
     ); 
    } 

    /** 
    * @return array customized attribute labels (name=>label) 
    */ 
    public function attributeLabels() { 
     return array(
      'id' => 'ID', 
      'instance_id' => 'Instance', 
      'website_id' => 'Website', 
      'link' => 'Link', 
      'title' => 'Title', 
      'created' => 'Created', 
      'updated' => 'Updated', 
      'status' => 'Status', 
     ); 
    } 

    /** 
    * 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, true); 
     $criteria->compare('instance_id', $this->instance_id); 
     $criteria->compare('website_id', $this->website_id); 
     $criteria->compare('link', $this->link, true); 
     $criteria->compare('title', $this->title, true); 
     $criteria->compare('created', $this->created); 
     $criteria->compare('updated', $this->updated); 
     $criteria->compare('status', $this->status); 

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

} 

的時候我會重新生成model,該construleunique將丟失

所以,我創建了一個新的model classUrlM,它擴展了url模型類,這個類KEPS我的所有增強功能:

<?php 

class UrlM extends Url { 

    const ACTIVE = 1; 

    public function rules() { 
     return array(
      array('link', 'unique'), 
     ); 
    } 

    public function relations() { 
     return array(
     ); 
    } 

} 

我如何Concat的從url模型類的規則與rulesurlm模型類?

相同的答案應該APLY爲今後的改進,如relations

回答

4

父類中的每一個功能 - 就像rules()relations()等,返回數組。所以你可以使用呼叫parent::rules(),然後加入「陣列」。

public function rules() { 
    $myParentRules=parent::rules(); 
    $myConcatRules=$myParentRules + array(
    array('link', 'unique'), 
); 
    return $myConcatRules; 
} 
+0

將嘗試此操作並看到 –

+0

+操作符不起作用,使用合併,分叉完美,10倍 –

0

array_merge會幫助你。

public function rules() { 
    $myRules = array(
     array('id', 'numerical', 'integerOnly'=>true), 
    ); 

    return array_merge(parent::rules(),$myRules); 

}