0

I have an Account Model which has some properties and relations, and i have an Reseller model. The point is that a reseller is actually an account only it has the possibility to have accounts beneath it.

What is the best approach to implement this, at first i had a special Reseller class with relations between them, but actually i just want an accounts class which if the account is a reseller uses the reseller class.

Account model

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

}

Reseller Model

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

}
TheWolfNL
  • 1,263
  • 1
  • 13
  • 29

3 Answers3

1

First keep in mind that ActiveRecord != models its easy to get confused, beware!

Also check this post

Ok now, you can have some factory method that gives you the class you need, Account or Reseller, depending on your logic, if not all acounts can be resellers you may also need some way to determine this. like a "is_reseller" column or similar.

Asgaroth
  • 4,274
  • 3
  • 20
  • 36
  • Well i think i need something like the factory method only i have no clue how to implement something like that in Yii. For the record: in the account table there is a boolean column "is_reseller". so i need to know how to make a reseller object when an account object is called upon. – TheWolfNL Oct 14 '12 at 23:23
  • Whats the idea behind all of this anyways? why do you need different classes? – Asgaroth Oct 14 '12 at 23:28
  • Mainly i want the reseller to have some additional relations, and keep the possibility to add some reseller specific functions for example storing settings a normal account shouldn't have. – TheWolfNL Oct 15 '12 at 00:03
  • the single table inheritance is what you need, it will be the same table just different classes with additional logic, see http://www.yiiframework.com/wiki/198/single-table-inheritance/ instead of type, you'd use 'is_reseller' and overwrite the relations method – Asgaroth Oct 15 '12 at 00:10
0

In the end i made a relation from accounts to itself and solved it.

 /**
 * @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'),
    );
}
TheWolfNL
  • 1,263
  • 1
  • 13
  • 29
0

I used a extended classes to a models to implemented a diferents methos and a unique prerequisite is add this function in a new class:

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