-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
ca91f03
to
43e5023
Compare
// 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(); |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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()); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
Replaced by #2984 |
Fix PHPORM-180
Fix #2935
In order to trigger the same events as Eloquent's
createOrFirst
, the process must be in theModel
class. So I created a newsaveOrFirst
method that needs to be public to be called by theEloquent\Query
class, but I want to keep it internal. I tried to add an option toModel::save
, but this method returns an boolean and I need the object if found.Checklist
Update documentation for new features