Add ActiveRecordFactory and refactor FactoryTrait#547
Add ActiveRecordFactory and refactor FactoryTrait#547Tigrov wants to merge 8 commits intoadd-instantiatefrom
ActiveRecordFactory and refactor FactoryTrait#547Conversation
Tigrov
commented
Mar 16, 2026
| Q | A |
|---|---|
| Is bugfix? | ✔️/❌ |
| New feature? | ✔️/❌ |
| Breaks BC? | ✔️ |
| Fixed issues |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## add-instantiate #547 +/- ##
===================================================
Coverage 100.00% 100.00%
- Complexity 657 658 +1
===================================================
Files 43 44 +1
Lines 1631 1632 +1
===================================================
+ Hits 1631 1632 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR introduces a centralized ActiveRecordFactory to support DI-based ActiveRecord instantiation, and refactors FactoryTrait to use a new static instantiate() entry point (replacing the previous per-instance withFactory() / createQuery() approach). It updates tests and documentation accordingly and is marked as a BC break.
Changes:
- Add
Yiisoft\ActiveRecord\ActiveRecordFactory(static registry) and refactorFactoryTraitto provideinstantiate(): staticvia that factory. - Update ActiveRecord stubs and tests to use DI-instantiated models (
::instantiate()/::query()) and add aMyServicetest dependency. - Update DI documentation to describe configuring
ActiveRecordFactoryand usinginstantiate()/query().
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
src/ActiveRecordFactory.php |
Adds a static factory registry used to create ActiveRecord instances via DI. |
src/Trait/FactoryTrait.php |
Replaces previous factory wiring with instantiate() delegating to ActiveRecordFactory. |
tests/ActiveRecordTest.php |
Refactors factory-related tests to use the new global factory approach and adds coverage for per-class factories. |
tests/Support/ConnectionHelper.php |
Simplifies factory creation to rely on container definitions only. |
tests/Support/MyService.php |
Adds a small DI dependency used by factory-enabled stubs/tests. |
tests/Stubs/ActiveRecord/OrderWithFactory.php |
Updates stub to require DI (MyService) and removes relation variant that manually created instances via a factory. |
tests/Stubs/ActiveRecord/CustomerWithFactory.php |
Updates stub to require DI (MyService) instead of receiving a factory in the constructor. |
tests/Driver/*/ActiveRecordTest.php |
Adds an import for ActiveRecordFactory (currently unused). |
docs/using-di.md |
Updates user docs to describe configuring ActiveRecordFactory and using instantiate() / query(). |
docs/traits/factory.md |
Updates trait docs to reflect instantiate() and removes outdated limitations/workarounds. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Adds a centralized ActiveRecordFactory to support DI-based ActiveRecord instantiation, and refactors FactoryTrait to rely on it (shifting from per-instance factory injection to a static/global factory registry).
Changes:
- Introduce
ActiveRecordFactory(static registry) and refactorFactoryTraitto implementinstantiate()via it. - Update stubs and tests to use
::instantiate()/::query()with initialized factories; add dedicatedActiveRecordFactoryTestand per-driver wrappers. - Update DI documentation to reflect the new configuration/usage flow.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/ActiveRecordFactory.php | New static registry used to create AR instances via yiisoft/factory. |
| src/Trait/FactoryTrait.php | Replaces per-instance factory wiring with instantiate() calling ActiveRecordFactory::create(). |
| tests/Support/MyService.php | Adds a small DI target used to validate dependency injection into AR constructors. |
| tests/Support/ConnectionHelper.php | Adjusts Factory construction (container now carries DB definition). |
| tests/Stubs/ActiveRecord/OrderWithFactory.php | Refactors stub to constructor-inject MyService; removes “factory instance relation” case. |
| tests/Stubs/ActiveRecord/CustomerWithFactory.php | Refactors stub to constructor-inject MyService (no longer accepts Factory). |
| tests/ActiveRecordTest.php | Updates factory-related tests to new global factory model; adds initFactory(). |
| tests/ActiveRecordFactoryTest.php | New unit tests for ActiveRecordFactory API and StrictFactory behavior. |
| tests/Driver/*/ActiveRecordFactoryTest.php | Adds driver-suite wrappers so the new factory tests run in each DB test suite. |
| docs/using-di.md | Rewrites DI guide to describe configuring ActiveRecordFactory and using instantiate() / query(). |
| docs/traits/factory.md | Updates trait docs to describe instantiate() usage (now backed by ActiveRecordFactory). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>