Download the PHP package yiister/yii2-mappable-ar without Composer
On this page you can find all versions of the php package yiister/yii2-mappable-ar. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yiister/yii2-mappable-ar
More information about yiister/yii2-mappable-ar
Files in yiister/yii2-mappable-ar
Package yii2-mappable-ar
Short Description It is an extension for Yii framework 2 that gives an ability to use identity map for any ActiveRecord model.
License BSD-3-Clause
Homepage https://github.com/yiister/yii2-mappable-ar
Informations about the package yii2-mappable-ar
Yii2 mappable ActiveRecord
It is an extension for Yii framework 2 that gives an ability to use identity map for any ActiveRecord model.
Russian documentation.
How it works
ActiveRecordTrait
overrides a find
method of model. This method creates a custom ActiveQuery
. When one
(all
) method is called, a got model (models) save to identityMap
as array of attributes (It saves a memory). The next requests return data without queries to data base.
By the way the next methods are allowed:
getById(integer $id, boolean $asArray = false)
- get a model or an array of attributes (It depends on second param value) by primary key;getByAttribute(string $attribute, string $value, boolean $asArray = false)
- get a model or an array of attributes (It depends on second param value) by unique attribute value;getMap()
- get all models fromidentityMap
as array of attributes;clearMap()
- clear anidentityMap
.
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require
section of your composer.json.
Setting
The extension supports the next settings:
idAttribute
- the primary key attribute (by defaultid
);identityMapMaxSize
- the maximum elements count in identityMap (by default-1
= no limit);uniqueAttributes
- array of attribute names that contains unique values. It is used at thegetByAttribute
method.
For example, for change a primary key attribute to key
add to your model public static $idAttribute = 'key';
.
Using
Just add use yiister\mappable\ActiveRecordTrait;
to your model for using an identityMap. You got all features after it.
Warn! If you have overridden find
method in your model you have to call activeRecordTraitFind()
method and work with its result.
Example: