Note that there are some explanatory texts on larger screens.

plurals
  1. POYii Model extending / overloading
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p><strong>Account model</strong></p> <pre><code>&lt;?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' =&gt; 40), array('name', 'length', 'max' =&gt; 45), array('invoice_id', 'length', 'max' =&gt; 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' =&gt; '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' =&gt; array(self::BELONGS_TO, 'Reseller', 'reseller_id'), 'contacts' =&gt; array(self::HAS_MANY, 'Contact', 'account_id'), 'domains' =&gt; array(self::HAS_MANY, 'Domain', 'account_id'), 'hostingAccounts' =&gt; array(self::HAS_MANY, 'HostingAccount', 'account_id'), 'users' =&gt; array(self::HAS_MANY, 'User', 'account_id'), ); } /** * @return array customized attribute labels (name=&gt;label) */ public function attributeLabels() { return array( 'id' =&gt; 'ID', 'reseller_id' =&gt; 'Reseller', 'name' =&gt; 'Name', 'invoice_id' =&gt; '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-&gt;compare('id', $this-&gt;id, true); $criteria-&gt;compare('reseller_id', $this-&gt;reseller_id, true); $criteria-&gt;compare('name', $this-&gt;name, true); $criteria-&gt;compare('invoice_id', $this-&gt;invoice_id, true); return new CActiveDataProvider($this, array( 'criteria' =&gt; $criteria, )); } /** * Adds UUID before the item is saved * */ public function beforeSave() { if ($this-&gt;isNewRecord) $this-&gt;id = new CDbExpression('UUID()'); return parent::beforeSave(); } } </code></pre> <p><strong>Reseller Model</strong></p> <pre><code>&lt;?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' =&gt; array(self::BELONGS_TO, 'Reseller', 'reseller_id'), 'contacts' =&gt; array(self::HAS_MANY, 'Contact', 'account_id'), 'domains' =&gt; array(self::HAS_MANY, 'Domain', 'account_id'), 'hostingAccounts' =&gt; array(self::HAS_MANY, 'HostingAccount', 'account_id'), 'users' =&gt; array(self::HAS_MANY, 'User', 'account_id'), 'accounts' =&gt; array(self::HAS_MANY, 'Account', 'reseller_id'), 'account' =&gt; array(self::BELONGS_TO, 'Account', 'account_id'), ); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload