Add initialization for unique identifiers in HasUuids#7727
Add initialization for unique identifiers in HasUuids#7727FaboBorgesLima wants to merge 4 commits intohyperf:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Adds trait-level initialization logic so models using HasUuids can have their unique identifier columns pre-populated automatically during model construction.
Changes:
- Added
initializeHasUuids()trait initializer to set UUIDs on columns returned byuniqueIds()when empty. - Generates UUIDs via existing
newUniqueId()(UUIDv7) during initialization.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| { | ||
| $columns = $this->uniqueIds(); | ||
| foreach ($columns as $column) { | ||
| if (in_array($column, $this->uniqueIds()) && ($this->{$column} === null || $this->{$column} === '')) { |
There was a problem hiding this comment.
Inside the foreach ($columns as $column) loop, in_array($column, $this->uniqueIds()) is redundant (the loop already iterates over uniqueIds()), and calling uniqueIds() again inside the loop adds unnecessary work and could behave inconsistently if uniqueIds() is overridden with non-deterministic logic. Use the $columns variable (or remove the membership check entirely) and keep the emptiness check only.
| if (in_array($column, $this->uniqueIds()) && ($this->{$column} === null || $this->{$column} === '')) { | |
| if ($this->{$column} === null || $this->{$column} === '') { |
| protected function initializeHasUuids(): void | ||
| { | ||
| $columns = $this->uniqueIds(); | ||
| foreach ($columns as $column) { | ||
| if (in_array($column, $this->uniqueIds()) && ($this->{$column} === null || $this->{$column} === '')) { | ||
| $this->{$column} = $this->newUniqueId(); | ||
| } | ||
| } |
There was a problem hiding this comment.
This change adds new runtime behavior (auto-populating unique ID columns during model initialization), but there is no test covering it. Consider adding/expanding a unit test (e.g., using ModelStubWithUuid) to assert that a new model instance gets a non-empty UUID assigned for the key column, and that providing an explicit key value is not overwritten.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Refactor UUID initialization logic to use the initializeHasUUID method for better clarity and efficiency.
|
应该不需要这个吧 |
Now it only triggers when inserting
You were completely right. Using initialize generates UUIDs on every instantiation (like hydrating select queries), causing unnecessary CPU overhead. |
No description provided.