Skip to content

Add ActiveRecordFactory and refactor FactoryTrait#547

Open
Tigrov wants to merge 8 commits intoadd-instantiatefrom
refactor-factory-trait
Open

Add ActiveRecordFactory and refactor FactoryTrait#547
Tigrov wants to merge 8 commits intoadd-instantiatefrom
refactor-factory-trait

Conversation

@Tigrov
Copy link
Member

@Tigrov Tigrov commented Mar 16, 2026

Q A
Is bugfix? ✔️/❌
New feature? ✔️/❌
Breaks BC? ✔️
Fixed issues

@Tigrov Tigrov added the severity:BC breaking Breaks backwards compatibility label Mar 16, 2026
@codecov
Copy link

codecov bot commented Mar 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (5ffe3a1) to head (4814ba8).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Tigrov Tigrov added the status:code review The pull request needs review. label Mar 16, 2026
@Tigrov Tigrov requested a review from a team March 16, 2026 12:05
@samdark samdark requested a review from Copilot March 16, 2026 12:10
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 refactor FactoryTrait to provide instantiate(): static via that factory.
  • Update ActiveRecord stubs and tests to use DI-instantiated models (::instantiate() / ::query()) and add a MyService test dependency.
  • Update DI documentation to describe configuring ActiveRecordFactory and using instantiate() / 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.

@Tigrov Tigrov requested a review from Copilot March 19, 2026 06:33
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 refactor FactoryTrait to implement instantiate() via it.
  • Update stubs and tests to use ::instantiate() / ::query() with initialized factories; add dedicated ActiveRecordFactoryTest and 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

severity:BC breaking Breaks backwards compatibility status:code review The pull request needs review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants