-
Notifications
You must be signed in to change notification settings - Fork 1.4k
PHPORM-180 Keep createOrFirst in 2 commands to simplify implementation #2984
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
Unfortunately, that doesn't solve the nested transaction issue reported initially #2720.
|
65418d2
to
181f2de
Compare
181f2de
to
b082fd1
Compare
* @param array $values The attributes to insert if no matching record is found | ||
*/ | ||
public function createOrFirst(array $attributes = [], array $values = []): Model | ||
public function firstOrCreate(array $attributes = [], array $values = []) |
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 not actually suggesting this, but it's amusing to me that this entire method body could be reduced to:
return (clone $this)->where($attributes)->first() ??
$this->getConnection()->getSession()?->isInTransaction()
? $this->create(array_merge($attributes, $values))
: $this->createOrFirst($attributes, $values);
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.
Sure, but it won't help me to use xdebug's step-by-step debugger.
Fix PHPORM-180
Alternative to #2980
Revert optimisation from #2742 in order to keep all the processes from Eloquent and simplify maintenance.
The main feature difference is that it's requiring a unique index whereas my previous implementation was only using the filter "attributes". This new implementation is closer to Laravel's implementation, it only removes the nested transaction and catch the MongoDB exception instead of the PDO exception.
Checklist