2012-10-14 142 views
0

我有一個帳戶模型,它有一些屬性和關係,我有一個經銷商模型。 問題是,經銷商實際上只是一個帳戶,它有可能在其下擁有帳戶。Yii模型擴展/超載

什麼是實現這一點, 起初我與他們之間的關係特殊的經銷商班上最好的辦法,但實際上我只是想它,如果該帳戶是經銷商使用經銷商類的賬戶類。

帳戶模式

<?php 

/** 
* This is the model class for table "account". 
* 
* The followings are the available columns in table 'account': 
* @property string $id 
* @property string $reseller_id 
* @property string $name 
* @property string $invoice_id 
* @property boolean $is_reseller 
* 
* The followings are the available model relations: 
* @property Reseller $reseller 
* @property Contact[] $contacts 
* @property Domain[] $domains 
* @property HostingAccount[] $hostingAccounts 
* @property User[] $users 
*/ 
class Account extends CActiveRecord { 

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

    /** 
    * @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('name', 'required'), 
      array('id, reseller_id', 'length', 'max' => 40), 
      array('name', 'length', 'max' => 45), 
      array('invoice_id', 'length', 'max' => 10), 
      // The following rule is used by search(). 
      // Please remove those attributes that should not be searched. 
      array('id, reseller_id, name, invoice_id', '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(
      'reseller' => array(self::BELONGS_TO, 'Reseller', 'reseller_id'), 
      'contacts' => array(self::HAS_MANY, 'Contact', 'account_id'), 
      'domains' => array(self::HAS_MANY, 'Domain', 'account_id'), 
      'hostingAccounts' => array(self::HAS_MANY, 'HostingAccount', 'account_id'), 
      'users' => array(self::HAS_MANY, 'User', 'account_id'), 
     ); 
    } 

    /** 
    * @return array customized attribute labels (name=>label) 
    */ 
    public function attributeLabels() { 
     return array(
      'id' => 'ID', 
      'reseller_id' => 'Reseller', 
      'name' => 'Name', 
      'invoice_id' => 'Invoice', 
     ); 
    } 

    /** 
    * 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('reseller_id', $this->reseller_id, true); 
     $criteria->compare('name', $this->name, true); 
     $criteria->compare('invoice_id', $this->invoice_id, true); 

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

    /** 
    * Adds UUID before the item is saved 
    * 
    */ 
    public function beforeSave() { 
     if ($this->isNewRecord) 
      $this->id = new CDbExpression('UUID()'); 

     return parent::beforeSave(); 
    } 

} 

代理商模式

<?php 

class Reseller extends Account 
{ 

    /** 
    * @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(
         'reseller' => array(self::BELONGS_TO, 'Reseller', 'reseller_id'), 
         'contacts' => array(self::HAS_MANY, 'Contact', 'account_id'), 
         'domains' => array(self::HAS_MANY, 'Domain', 'account_id'), 
         'hostingAccounts' => array(self::HAS_MANY, 'HostingAccount', 'account_id'), 
         'users' => array(self::HAS_MANY, 'User', 'account_id'), 
      'accounts' => array(self::HAS_MANY, 'Account', 'reseller_id'), 
      'account' => array(self::BELONGS_TO, 'Account', 'account_id'), 
     ); 
    } 

} 

回答

0

最後我從帳戶本身和關係中解決了它。

/** 
* @return array relational rules. 
*/ 
public function relations() { 
    return array(
     'reseller' => array(self::BELONGS_TO, 'Account', 'account_id'), 
     'users' => array(self::HAS_MANY, 'User', 'account_id'), 
     'accounts' => array(self::HAS_MANY, 'Account', 'account_id'), 
    ); 
} 
1

首先記住,ActiveRecord != models其容易混淆,當心!

還要檢查this post

現在好了,你可以有一些factory method,爲您提供您所需要的類,帳戶或經銷商,這取決於你的邏輯,如果不是所有acounts可經銷商可能還需要一些方法來確定這一點。像「is_reseller」列或類似的。

+0

嗯,我想我需要像工廠方法只我不知道如何在Yii中實現類似的東西。 記錄:在帳戶表中有一個布爾列「is_reseller」。所以我需要知道如何在調用帳戶對象時創建經銷商對象。 – TheWolfNL

+0

無論如何,這背後的想法是什麼?你爲什麼需要不同的課程? – Asgaroth

+0

主要是我希望經銷商有一些額外的關係,並保留添加一些經銷商特定功能的可能性,例如存儲普通賬戶不應該有的設置。 – TheWolfNL

0

我用了一個擴展類的模型來實現的diferents methos和獨特的先決條件是在一個新的類添加此功能:

public static function model($className=__CLASS__){ 
return parent::model($className); 
}