Download the PHP package sam-it/yii2-virtual-fields without Composer
On this page you can find all versions of the php package sam-it/yii2-virtual-fields. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sam-it/yii2-virtual-fields
More information about sam-it/yii2-virtual-fields
Files in sam-it/yii2-virtual-fields
Package yii2-virtual-fields
Short Description Implementation of virtual fields for Yii2 AR
License MIT
Informations about the package yii2-virtual-fields
yii2-virtual-fields
Implementation of virtual fields for Yii2 AR
To work this library needs to change 2 parts of the Yii2 ORM.
- The model definitions
- The query implementation
Configuration
The change to ActiveQuery
are simple and can be applied using a trait or a behavior. In case you did not subclass
ActiveQuery
you can choose to attach the behavior dynamically.
Since Yii uses the DI container to create the object, it is also possible to add the behavior globally by defining it in the DI container. Virtual fields allow you to define model attributes as SQL fragments. The advantage of this implementation is that it supports both lazy and greedy loading.
Usage
The library will then take care of everything:
If lazy loading is not implemented and an attribute is used lazily, an exception will be thrown. If greedy loading is not implemented and the field is added to select the normal Yii2 / SQL exception is thrown.
We found these classes greatly helped us in reducing the number of queries (implementing getPostCount()
on Author
is not ideal).
To maximize compatibility and minimize issues we chose not to use joins, since they can potentially affect the number of records. In a number of cases the resulting query plan could be less than optimal.
We chose not to overload ActiveQuery::select()
to support virtual fields. Reason for this is the fact that it changes the semantics of *
; *
would not by default include all virtual fields.