Skip to content

Add UUID initialization in model save method#25

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

Add UUID initialization in model save method#25
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:24
@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

This PR updates the HasUuids model concern to automatically populate UUID-based identifier columns when a model is saved, so UUID primary keys (and other uniqueIds) don’t need to be manually assigned by callers.

Changes:

  • Added a helper method to assign new UUIDs to configured uniqueIds columns when missing.
  • Overrode save() in the UUID trait to run the initialization before delegating to the base model save.

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

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.

The helper method name initialize is very generic and can easily collide with other traits or model methods, and it is also easy to confuse with the model’s trait initializer convention (initialize{TraitName} collected in Model::bootTraits). Renaming this to something UUID-specific (e.g., initializeUniqueIds / setUniqueIdsIfMissing) will reduce the risk of trait method collisions and improve readability.

Copilot uses AI. Check for mistakes.
Comment on lines +75 to +76
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.

Inside initialize(), the in_array($column, $this->uniqueIds()) check is redundant when the caller already passes $this->uniqueIds(), and it also recomputes uniqueIds() on every iteration. Consider either removing the membership check entirely or computing uniqueIds() once before the loop.

Suggested change
foreach ($columns as $column) {
if (in_array($column, $this->uniqueIds()) && ! $this->{$column}) {
$uniqueIds = $this->uniqueIds();
foreach ($columns as $column) {
if (in_array($column, $uniqueIds, true) && ! $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 change alters persistence behavior by auto-populating UUID columns during save(), but there are no tests covering the new behavior. Please add/update tests to assert (1) a new model gets a UUID assigned before insert, and (2) saving an existing model does not regenerate or change its UUID/primary key.

Copilot uses AI. Check for mistakes.

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 assign UUIDs unconditionally can mutate primary keys (or other uniqueIds) during update operations. In particular, if an existing model instance has a falsey/missing key attribute (e.g., loaded with a select that omits the PK), this will generate a new UUID and the subsequent update can target the wrong row (or no row) while still returning true. Consider only generating IDs for inserts (e.g., guard with if (! $this->exists)), or move the logic to the insert path (such as performInsert) so updates never auto-regenerate identifiers.

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

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