PHPackages                             laszlovl/yii2-staticactiverecord - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. laszlovl/yii2-staticactiverecord

ActiveYii2-extension[Database &amp; ORM](/categories/database)

laszlovl/yii2-staticactiverecord
================================

A version of Yii2's ActiveRecord with various performance improvements at the cost of slightly less flexibility

v1.2(11y ago)847BSD-3-ClausePHP

Since May 2Pushed 11y ago2 watchersCompare

[ Source](https://github.com/laszlovl/yii2-staticactiverecord)[ Packagist](https://packagist.org/packages/laszlovl/yii2-staticactiverecord)[ RSS](/packages/laszlovl-yii2-staticactiverecord/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

yii2-staticactiverecord
=======================

[](#yii2-staticactiverecord)

A faster but slightly less flexible version of Yii2's ActiveRecord.

[![Build Status](https://camo.githubusercontent.com/4f4d9bf703b89ade45243be77a1abc12027da6d49446107a87af69d61861020e/68747470733a2f2f7472617669732d63692e6f72672f6c61737a6c6f766c2f796969322d7374617469636163746976657265636f72642e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/laszlovl/yii2-staticactiverecord)

DESCRIPTION
-----------

[](#description)

Yii2 is a great framework, and its flexibility is a big part of that. As usual though, flexibility comes at the cost of performance, and sometimes the performance impact might not be worth the tradeoff.

This code focusses specifically on Yii2's ActiveRecord. Because of ActiveRecord's flexibility, a lot of calculations have to be done at runtime: look up the corresponding database schema, derive the list of attributes from that, determine the list of safe attribute for each scenario based on the model's `rules()`, etcetera.

In Yii2's architecture, most of these calculations are not only done once (per request) for every one of your ActiveRecord *classes*, but for every one of your ActiveRecord *instances*.

Take this code as example:

```
foreach (Cat::find()->limit(100)->all() as $cat) {
    echo $cat->owner;
}
```

The call to `->owner` will be forwarded to `__get()`, which will call `hasAttribute()`, which will call `attributes()`, which will call `getTableSchema()`, which will use the DI container to retrieve a reference to the `db` component, and so on. All of this just to determine whether your Cat class has a native property `$owner` or that it should be resolved as ActiveRecord property, or perhaps as a relation. This is done not just once, but 100 times: again for every one of your Cat instances.

Is all this necessary? This of course depends on the way your application is designed. For example, it's very possible to attach behaviors to objects at runtime, causing one of your cats to have a `owner` relation, while another one doesn't. Or to override `attributes()` to completely hide the `owner`property based on the value of one of the other properties of the instance.

But in a lot of applications this isn't the case: while different instances of a class would of course have different *values*, their *metadata* would be **static**: either all of your Cats have a property `owner`, or none of them do. If one of your cat instances is found to have the `MiowBehavior`, all other cats would have it as well.

If this is indeed the case for your application, we can cache this metadata on the class level: once it's calculated for one instance of a class, for all other instances it can be looked up instantly. This extension's `ActiveRecord` does just that, by using a static class variable to cache the result of a function between all instances of the containing class.

This can enormously benefit your application's performance, especially for requests that handle a lot of instances of the same class, like a GridView or ListView with a large pagination size, or an API call on a resource collection.

BENCHMARKS
----------

[](#benchmarks)

The extension comes with a benchmark tool to measure the performance improvements for a few very simple scenarios. In all cases, 1000 instances of the same class are created.

```
~/yii2-staticactiverecord/benchmark$ ./yii benchmark

Benchmarking benchmarkGetProperty...
Regular ActiveRecord: 0.0089842319488525
Static ActiveRecord:  0.0069756507873535
Improvement:          23%

Benchmarking benchmarkSetProperty...
Regular ActiveRecord: 0.009577751159668
Static ActiveRecord:  0.0076509952545166
Improvement:          21%

Benchmarking benchmarkValidate...
Regular ActiveRecord: 0.046598815917969
Static ActiveRecord:  0.03521466255188
Improvement:          25%

```

The sum of the performance improvements in a single request of course highly depends on your specific application, but I've been able to reduce the response time for one of my application's heavy requests by more than 50%.

TESTS
-----

[](#tests)

The extension re-uses Yii's existing ActiveRecordTest suite, overriding the default ActiveRecord implementation with the one in this extension. This shows that none of the testcases included in Yii are broken by this extension's behavior.

CAVEATS
-------

[](#caveats)

As was said, this extension is not for everyone. Some signs that it might not be suitable for a specific class:

- The class overrides any of the functions `getTableSchema()`, `primaryKey()`, `attributes()`, `hasAttribute()`, `scenarios()`
- There is a reference to `$this` in one of these functions in the class: `rules()`, `behaviors()`, `tableName()`
- Your application attaches behaviors to the class at runtime
- Your application calls `getTableSchema($refresh=true)` on the class' table

INSTALLATION
------------

[](#installation)

Via composer: `composer require laszlovl/yii2-staticactiverecord`

After that, simply extend your ActiveRecord classes from `lvl\staticactiverecord\db\ActiveRecord` instead of `yii\db\ActiveRecord`.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~5 days

Total

2

Last Release

4029d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/92b3d85c0d75aa9473250de89f6b1dc4260e2ff5887bab8acc97e5c97c84c054?d=identicon)[laszlovl](/maintainers/laszlovl)

---

Top Contributors

[![laszlovl](https://avatars.githubusercontent.com/u/12112486?v=4)](https://github.com/laszlovl "laszlovl (8 commits)")

---

Tags

yii2activerecord

### Embed Badge

![Health badge](/badges/laszlovl-yii2-staticactiverecord/health.svg)

```
[![Health](https://phpackages.com/badges/laszlovl-yii2-staticactiverecord/health.svg)](https://phpackages.com/packages/laszlovl-yii2-staticactiverecord)
```

###  Alternatives

[nhkey/yii2-activerecord-history

Storage history of changes to ActiveRecord

46143.4k1](/packages/nhkey-yii2-activerecord-history)[yii2tech/illuminate

Yii2 to Laravel Migration Package

11315.1k](/packages/yii2tech-illuminate)[spinitron/yii2-dynamic-ar

Extends Yii ActiveRecord for Maria Dynamic Columns

576.8k](/packages/spinitron-yii2-dynamic-ar)[jlorente/yii2-activerecord-inheritance

ActiveRecord Inheritance is an util to provide the Class Table Inheritance Pattern the to the Yii2 framework. It fakes inheritance between two ActiveRecord classes.

184.2k](/packages/jlorente-yii2-activerecord-inheritance)[execut/yii2-1c-odata

Yii2 component for work with 1C oData via activeRecord

101.3k](/packages/execut-yii2-1c-odata)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
