Extended ActiveRecord class for Yii2
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.
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();
No stable releases.
Comments