Skip to content

Add ActiveRecordFactory and refactor FactoryTrait#547

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

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

Conversation

@Tigrov
Copy link
Copy Markdown
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
Copy Markdown

codecov Bot commented Mar 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
⚠️ Please upload report for BASE (add-instantiate@a063343). Learn more about missing BASE report.

Additional details and impacted files
@@                 Coverage Diff                 @@
##             add-instantiate      #547   +/-   ##
===================================================
  Coverage                   ?   100.00%           
  Complexity                 ?       658           
===================================================
  Files                      ?        44           
  Lines                      ?      1632           
  Branches                   ?         0           
===================================================
  Hits                       ?      1632           
  Misses                     ?         0           
  Partials                   ?         0           

☔ 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
Copy Markdown

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.

Comment thread tests/Driver/Mssql/ActiveRecordTest.php Outdated
Comment thread tests/Driver/Oracle/ActiveRecordTest.php Outdated
Comment thread tests/ActiveRecordTest.php
Comment thread tests/Support/MyService.php
Comment thread tests/Stubs/ActiveRecord/CustomerWithFactory.php Outdated
Comment thread tests/Driver/Sqlite/ActiveRecordTest.php Outdated
Comment thread tests/Driver/Pgsql/ActiveRecordTest.php
Comment thread tests/Driver/Mysql/ActiveRecordTest.php Outdated
Comment thread docs/using-di.md
@Tigrov Tigrov requested a review from Copilot March 19, 2026 06:33
Copy link
Copy Markdown

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.

Comment thread src/ActiveRecordFactory.php
Comment thread src/ActiveRecordFactory.php Outdated
Comment thread tests/ActiveRecordTest.php
Comment thread tests/ActiveRecordTest.php
Comment thread docs/traits/factory.md
Tigrov and others added 2 commits March 19, 2026 15:11
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 3, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 00bb53d0-c819-44c7-b354-6a16e2686571

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor-factory-trait

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

4 participants