【问题标题】:Yii Model extending / overloadingYii 模型扩展/重载
【发布时间】:2012-10-14 22:37:06
【问题描述】:

我有一个具有一些属性和关系的帐户模型,并且我有一个经销商模型。 关键是经销商实际上是一个帐户,只是它有可能在其下拥有帐户。

实现这一点的最佳方法是什么, 起初我有一个特殊的 Reseller 类,它们之间有关系,但实际上我只想要一个 accounts 类,如果该帐户是经销商,则使用 reseller 类。

帐户模型

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

}

【问题讨论】:

    标签: php class model yii extending-classes


    【解决方案1】:

    首先要记住ActiveRecord != models很容易混淆,小心!

    同时检查this post

    现在,您可以拥有一些 factory method 来为您提供所需的类,帐户或经销商,具体取决于您的逻辑,如果不是所有帐户都可以是经销商,您可能还需要一些方法来确定这一点。像“is_reseller”列或类似的。

    【讨论】:

    • 好吧,我想我需要类似工厂方法的东西,只是我不知道如何在 Yii 中实现类似的东西。作为记录:在帐户表中有一个布尔列“is_reseller”。所以我需要知道如何在调用帐户对象时创建经销商对象。
    • 这一切背后的想法是什么?为什么需要不同的课程?
    • 主要是我希望经销商有一些额外的关系,并保留添加一些经销商特定功能的可能性,例如存储普通帐户不应该有的设置。
    • 单表继承是你所需要的,它将是同一个表,只是不同的类有额外的逻辑,见yiiframework.com/wiki/198/single-table-inheritance而不是类型,你会使用'is_reseller'并覆盖关系方法
    【解决方案2】:

    最后我建立了账户与自身的关系并解决了它。

     /**
     * @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'),
        );
    }
    

    【讨论】:

      【解决方案3】:

      我对模型使用了扩展类来实现不同的方法,唯一的先决条件是在新类中添加此功能:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-26
        • 2013-03-19
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        相关资源
        最近更新 更多