platx/yii2-active-record

Extended ActiveRecord class for Yii2

Installs: 2 015

Dependents: 0

Suggesters: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

Type:yii2-extension

dev-master 2016-06-03 07:04 UTC

README

Extended ActiveRecord class for Yii2

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist platx/yii2-active-record "*"

or add

"platx/yii2-active-record": "*"

to the require section of your composer.json file.

Usage

Once the extension is installed, simply use it in your code by :

class YourModel extends \platx\activerecord\ActiveRecord 
{
    public function rules()
    {
        return \yii\helpers\ArrayHelper::merge(parent::rules(), [
            // here put your validation rules
        ]);
    }
    public function behaviors()
    {
        return \yii\helpers\ArrayHelper::merge(parent::behaviors(), [
            // here put your behaviors configs
        ]);
    }
    protected function filterAttributes()
    {
        parent::filterAttributes();
        // Here you may filter different attributes in your model, like:
        $this->_mainQuery->andFilterWhere(['your_attribute' => $this->your_attribute]);
    }
}

For find record in DB by ID attribute, use this code:

$model = YourModel::findByPk($id);

where $id - integer, record ID.

If you want to know attribute has been modified or no, you can use following code:

$result = $model->isChanged('attribute_name');

where $model - object of your model class, attribute_name - name of model attribute for checking.

To get data as array for dropdown, radio or checkbox list, try this:

$dataArray = YourModel::listAll();

To get query with filtered attributes, use this code:

$query = $model->buildQuery();