From c2e0485e0a2c0e0c780459a202e65b705ac7fa60 Mon Sep 17 00:00:00 2001 From: skepticspriggan <91023755+skepticspriggan@users.noreply.github.com> Date: Fri, 16 Feb 2024 23:11:56 +0100 Subject: [PATCH] Explain why DI fails sometimes and how to fix this (#20010) (#20108) * Explain why DI fails sometimes and how to fix this (#20010) * Explain why AR does not support DI by default and how to support it (#20010) --- docs/guide/db-active-record.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/docs/guide/db-active-record.md b/docs/guide/db-active-record.md index e87727469bb..e77317214e5 100644 --- a/docs/guide/db-active-record.md +++ b/docs/guide/db-active-record.md @@ -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 When calling [[yii\db\ActiveRecord::refresh()|refresh()]] to refresh an Active Record instance, the