Skip to content

Commit

Permalink
Explain why AR does not support DI by default and how to support it (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
skepticspriggan committed Feb 16, 2024
1 parent 9f7d515 commit 166a4ad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
12 changes: 0 additions & 12 deletions docs/guide/concept-di-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -396,18 +396,6 @@ class HotelController extends Controller
}
```

Note that not all core code supports DI due to performance concerns. You can add support if needed by overriding
the method responsible for instantiating the class and making sure it uses [[Yii::createObject()]].

For example, you can add DI support to [[yii\db\ActiveRecord]] models by overriding the `instantiate()` method:

```php
public static function instantiate($row)
{
return Yii::createObject(static::class);
}
```

Advanced Practical Usage <span id="advanced-practical-usage"></span>
---------------

Expand Down
11 changes: 11 additions & 0 deletions docs/guide/db-active-record.md
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,17 @@ life cycle will happen:
> - [[yii\db\ActiveRecord::updateCounters()]]
> - [[yii\db\ActiveRecord::updateAllCounters()]]
> Note: DI is not supported by default due to performance concerns. You can add support if needed by overriding
> the [[yii\db\ActiveRecord::instantiate()|instantiate()]] method to instantiate the class via [[Yii::createObject()]]:
>
> ```php
> public static function instantiate($row)
> {
> return Yii::createObject(static::class);
> }
> ```
### Refreshing Data Life Cycle <span id="refreshing-data-life-cycle"></span>
When calling [[yii\db\ActiveRecord::refresh()|refresh()]] to refresh an Active Record instance, the
Expand Down

0 comments on commit 166a4ad

Please sign in to comment.