Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPORM-180 Trigger events in Model::createOrFirst #2980

Closed
wants to merge 1 commit into from

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented May 27, 2024

Fix PHPORM-180
Fix #2935

In order to trigger the same events as Eloquent's createOrFirst, the process must be in the Model class. So I created a new saveOrFirst method that needs to be public to be called by the Eloquent\Query class, but I want to keep it internal. I tried to add an option to Model::save, but this method returns an boolean and I need the object if found.

Checklist

  • Add tests and ensure they pass
  • Add an entry to the CHANGELOG.md file
  • Update documentation for new features

@GromNaN GromNaN force-pushed the PHPORM-180 branch 2 times, most recently from ca91f03 to 43e5023 Compare May 28, 2024 08:53
src/Eloquent/Builder.php Fixed Show fixed Hide fixed
@GromNaN GromNaN changed the title PHPORM-180 PHPORM-180 Trigger events in Model::createOrFirst May 28, 2024
@GromNaN GromNaN changed the base branch from 4.4 to 4.3 May 28, 2024 08:58
@GromNaN GromNaN changed the base branch from 4.3 to 4.4 May 28, 2024 08:58
@GromNaN GromNaN added the bug label May 28, 2024
@GromNaN GromNaN requested a review from jmikola May 28, 2024 09:30
@GromNaN GromNaN marked this pull request as ready for review May 28, 2024 09:30
@GromNaN GromNaN requested a review from a team as a code owner May 28, 2024 09:30
// If the model has an incrementing key, we can use the "insertGetId" method on
// the query builder, which will give us back the final inserted ID for this
// table from the database. Not all tables have to be incrementing though.
$attributes = $this->getAttributesForInsert();
Copy link
Member

Choose a reason for hiding this comment

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

Why does the comment above refer to insertGetId()? It's not clear to me how that's getting used here, unless it's through getAttributesForInsert().

Does getAttributesForInsert respect modifications from earlier model methods such as setUniqueIds() and updateTimestamps()? I assume this all pulls from the same state.


return $model;
});
return $instance->saveOrFirst($attributes);
Copy link
Member

Choose a reason for hiding this comment

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

I don't recall the original reason for introducing createOrFirst() in #2742 (PHPORM-139). It looks like this was necessary to avoid some issue with transactions.

Just wanted to confirm that a custom implementation is still required at all, given that we'll no longer be using findAndModify.

Copy link
Member Author

@GromNaN GromNaN May 28, 2024

Choose a reason for hiding this comment

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

We are still using findAndModify, but in Query\Builder::findAndModify.

The call chain is:

  • Model::createOrFirst
  • Eloquent\Query::createOrFirst
  • Model::saveOrFirst
  • Query\Builder::findAndModify
  • Collection::findAndModify

/** @internal Not part of Laravel Eloquent API. Use raw findOneAndModify if necessary */
public function saveOrFirst(array $criteria): ?static
{
$this->mergeAttributesFromCachedCasts();
Copy link
Member

Choose a reason for hiding this comment

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

I assume much of the contents for this method were lifted from an existing method in Eloquent. If so, can we reference that in the method doc block?

I'm a little worried that if anything changes upstream we'll probably need to find out the hard way that this needs to be updated, but that may be unavoidable.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm also concerned that we have a lot of duplicated code that is hard to understand and we are not sure we can maintain in the long-term.
Creating a simple alternative PR: #2984


$connection = $query->getConnection();
if (! $this->getConnectionName() && $connection) {
$this->setConnection($connection->getName());
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason for inheriting the query's connection? IS this necessary for dispatching the created event?

}

// If the model is successfully saved, we need to do a few more things once
// that is done. We will call the "saved" method here to run any actions
Copy link
Member

Choose a reason for hiding this comment

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

I don't see any reference to a saved method. Is this comment accurate?

tests/ModelTest.php Show resolved Hide resolved
@GromNaN
Copy link
Member Author

GromNaN commented May 30, 2024

Replaced by #2984

@GromNaN GromNaN closed this May 30, 2024
@GromNaN GromNaN deleted the PHPORM-180 branch May 30, 2024 16:01
@GromNaN GromNaN added this to the 4.4 milestone May 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

createOrFirst is not triggering creating nor created model events
2 participants