Skip to content

Add ActiveRecord::instantiate() method#546

Open
Tigrov wants to merge 2 commits intomasterfrom
add-instantiate
Open

Add ActiveRecord::instantiate() method#546
Tigrov wants to merge 2 commits intomasterfrom
add-instantiate

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 (e686a15) to head (5ffe3a1).
⚠️ Report is 2 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff             @@
##              master      #546   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
- Complexity       655       657    +2     
===========================================
  Files             43        43           
  Lines           1629      1631    +2     
===========================================
+ Hits            1629      1631    +2     

☔ 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 11:34
@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 adds a new ActiveRecord::instantiate() static method that uses ReflectionClass::newInstanceWithoutConstructor() to create ActiveRecord instances without invoking the constructor. This removes the previous limitation where ActiveRecord models with required constructor parameters couldn't be used with ActiveRecord::query(), RepositoryTrait, or relations that reference class names.

Changes:

  • Added instantiate() to ActiveRecordInterface and implemented it in AbstractActiveRecord using reflection to bypass the constructor
  • Updated ActiveQuery constructor and EventsTrait::query() to use instantiate() instead of new $modelClass()
  • Updated tests from expecting ArgumentCountError to asserting successful query/relation behavior, and updated documentation to reflect the new behavior

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/ActiveRecordInterface.php Added instantiate(): static method to the interface
src/AbstractActiveRecord.php Default implementation using ReflectionClass::newInstanceWithoutConstructor()
src/ActiveQuery.php Changed constructor to use $modelClass::instantiate() instead of new $modelClass()
src/Trait/EventsTrait.php Changed query() to use instantiate() instead of direct construction
tests/ActiveRecordTest.php Updated tests to verify constructors with required params now work with queries/relations
docs/create-model.md Replaced limitations section with a note about constructor not being called

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@Mister-42
Copy link

Thanks @Tigrov . I have tested this branch after my issue in #542, and it worked perfectly fine after adding suggested code. Good job

public static function instantiate(): static
{
	return new static();
}

@Tigrov Tigrov added status:ready for merge The pull request is OK to be merged. and removed status:code review The pull request needs review. labels Mar 24, 2026
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:ready for merge The pull request is OK to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants