2017 © Pedro Peláez
 

library yii2-model-setup

Configure all the model aspects in one place.

image

minity/yii2-model-setup

Configure all the model aspects in one place.

  • Monday, October 23, 2017
  • by AntonTyutin
  • Repository
  • 0 Watchers
  • 0 Stars
  • 6 Installations
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

In-one-place configuration for Yii2 models

If you feel uncomfortable to looking up dozen of methods while adding or checking up a property of Yii2 model, consider to use this traits., (*1)

Example

Usually, creating any ActiveRecord model we must define several methods, such tableName(), attributeLabels(), attributeHints(), rules() and fields()., (*2)

Using \Minity\ModelSetup\ActiveRecordConfigurationTrait all we need is define setup() method as below, (*3)

<?php

use Minity\ModelSetup\ActiveRecordConfigurationTrait;
use yii\db\ActiveRecord;

class Record extends ActiveRecord
{
    use ActiveRecordConfigurationTrait;

    protected static function setup() {
        return [
            'tableName' => '{{%record}}',
            'attributes' => [
                'field1' => [
                    'label' => 'Record Field 1',
                    'hint' => 'Record Hint 1',
                    'toArray' => 'record_field_1',
                    'rules' => ['required', ['number', 'min' => 0]],
                ],
                'field2' => [
                    'label' => 'Record Field 2',
                    'hint' => 'Record Hint 2',
                    'toArray' => true, // the same as 'field2'
                    'rules' => ['string'],
                ],
                'field3' => [
                    'label' => 'Record Field 3',
                    //'toArray' => false, // default
                    'rules' => ['safe'],
                ],
            ],
            'relations' => [
                'rel1' => ['hasOne', AnotherRecord::className(), ['id' => 'field1']],
                'rel2' => ['hasMany', ViaRecord::className(), ['id' => 'via_id'],
                    'viaTable' => ['junction_table', ['rec_id' => 'id']],
                ],
            ]
        ];
    }
}

Pay attention to the relations section. You can see there a declarative way to define relations, so you can get ActiveQuery object by calling $model->getRel1() or get ActiveRecord by calling $model->rel1., (*4)

The Versions

23/10 2017

dev-master

9999999-dev https://bitbucket.org/minity/yii2-model-setup

Configure all the model aspects in one place.

  Sources   Download

MIT

The Development Requires

23/10 2017

v0.1.0

0.1.0.0 https://bitbucket.org/minity/yii2-model-setup

Configure all the model aspects in one place.

  Sources   Download

MIT

The Development Requires