Skip to content

Add UUID initialization in model save method#26

Closed
FaboBorgesLima wants to merge 1 commit intohyperf:masterfrom
FaboBorgesLima:patch-1
Closed

Add UUID initialization in model save method#26
FaboBorgesLima wants to merge 1 commit intohyperf:masterfrom
FaboBorgesLima:patch-1

Conversation

@FaboBorgesLima
Copy link
Copy Markdown

No description provided.

Copilot AI review requested due to automatic review settings March 26, 2026 05:25
@github-actions
Copy link
Copy Markdown

Hi, this is a READ-ONLY repository, please submit your PR on the https://github.com/hyperf/hyperf repository.

This Pull Request will close automatically.

Thanks!

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 automatic UUID initialization for models using HasUuids by generating missing unique IDs during save().

Changes:

  • Introduces an initialize() helper to populate unique-id columns when empty.
  • Overrides save() in HasUuids to run UUID initialization before delegating to the base model save.

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


public function save(array $options = []): bool
{
$this->initialize($this->uniqueIds());
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

Overriding save() to always (re)initialize unique IDs can unintentionally mutate existing records on update. If a model already exists and a unique-id column isn't loaded (or is intentionally set to a falsy value), this will generate a new UUID and mark the attribute dirty, causing an UPDATE that changes a supposedly stable identifier. Consider only initializing unique IDs for new models (e.g., when ! $this->exists) or hooking into the insert/creating path instead of every save.

Suggested change
$this->initialize($this->uniqueIds());
if (! $this->exists) {
$this->initialize($this->uniqueIds());
}

Copilot uses AI. Check for mistakes.
Comment on lines +73 to +80
protected function initialize(array $columns): void
{
foreach ($columns as $column) {
if (in_array($column, $this->uniqueIds()) && ! $this->{$column}) {
$this->{$column} = $this->newUniqueId();
}
}
}
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

initialize() is a very generic method name in a codebase where models already have an initializeTraits() mechanism and a convention of initialize{TraitName} initializers. Renaming this helper to something more specific (e.g., initializeUniqueIds / initializeUuids) would reduce the risk of name collisions in consuming models/traits and make the call site clearer.

Copilot uses AI. Check for mistakes.
protected function initialize(array $columns): void
{
foreach ($columns as $column) {
if (in_array($column, $this->uniqueIds()) && ! $this->{$column}) {
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

initialize() is currently called with $this->uniqueIds(), but inside the loop it re-checks in_array($column, $this->uniqueIds()) on every iteration. That condition is redundant (and recomputes uniqueIds() repeatedly); simplifying it would make the intent clearer and avoid unnecessary work.

Suggested change
if (in_array($column, $this->uniqueIds()) && ! $this->{$column}) {
if (! $this->{$column}) {

Copilot uses AI. Check for mistakes.
Comment on lines +82 to +86
public function save(array $options = []): bool
{
$this->initialize($this->uniqueIds());

return parent::save($options);
Copy link

Copilot AI Mar 26, 2026

Choose a reason for hiding this comment

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

This PR introduces new behavior (UUID assignment during save()), but current tests only assert the format returned by newUniqueId(). Adding a test that saving a new model using HasUuids auto-populates the configured unique-id columns would help prevent regressions, especially around create vs update behavior.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants