0

いくつかのプロパティとリレーションを持つアカウントモデルがあり、リセラーモデルがあります。重要なのは、リセラーは実際にはアカウントであり、その下にアカウントを持つ可能性があるということです。

これを実装するための最良のアプローチは何ですか。最初は、それらの間に関係がある特別なリセラークラスがありましたが、実際には、アカウントがリセラーの場合はリセラークラスを使用するアカウントクラスが必要です。

アカウントモデル

<?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'),
        );
    }

}
4

3 に答える 3

1

まず、 ActiveRecord!=モデルは混乱しやすいことを覚えておいてください。注意してください!

この投稿もチェックしてください

さて、ロジックに応じて、必要なクラス、アカウントまたはリセラーを提供するファクトリメソッドを使用できます。すべてのアカウントがリセラーになれない場合は、これを決定するための何らかの方法も必要になる場合があります。「is_reseller」列などのように。

于 2012-10-14T23:04:25.533 に答える
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'),
    );
}
于 2012-11-03T14:48:10.103 に答える
0

モデルに拡張クラスを使用して、さまざまな方法を実装しました。独自の前提条件は、この関数を新しいクラスに追加することです。

public static function model($className=__CLASS__){
return parent::model($className);
}
于 2014-04-15T10:25:58.860 に答える