Closed elnur-ibr closed 5 years ago
Why would update Shop
be expected here?
@driesvints it was mistype. Corrected.
Heh, I'm not sure that setKeysForSaveQuery
is meant to be used like this. Can't you just overwrite the runSoftDelete
method if you want?
Yes you can overwrite runSoftDelete
but I think if I am overwrite Model::setKeysForSaveQuery
so SoftDelete::runSoftDelete
should take it into account.
Please see below setKeysForSaveQuery
and runSoftDelete
methods below. The where part practically same.
protected function setKeysForSaveQuery(Builder $query)
{
$query->where($this->getKeyName(), '=', $this->getKeyForSaveQuery());
return $query;
}
protected function runSoftDelete()
{
$query = $this->newModelQuery()->where($this->getKeyName(), $this->getKey());
...
}
...
The question is if this has side effects. @staudenmeir do you maybe see any from the changes above?
I agree that this should be changed. We should also use setKeysForSaveQuery()
in SoftDeletes::performDeleteOnModel()
to match Model::performDeleteOnModel()
.
An example where the current behavior is incorrect:
$user = User::find(1);
$user->id = 2;
$user->delete();
This works without SoftDeletes
but not with it.
So should I create new pull request ? And update both SoftDeletes::performDeleteOnModel() and SoftDelete::runSoftDelete ?
Yes, please create a PR.
@staudenmeir thanks for verifying! :)
PR was merged.
Description:
SoftDelete::runSoftDelete does not take into account overridden Model::setKeysForSaveQuery method. I use this for multi-tenancy purposes. Or for use composite primary keys.
Model:
When running below code:
Expected query:
UPDATE shop SET deleted_at = ?, Shop.updated_at = ? WHERE shop_id = ? AND user_id = ?
Actual query:
UPDATE shop SET deleted_at = ?, SHOP.updated_at = ? WHERE shop_id = ?
Solution is to replace below line
To
$query = $this->setKeysForSaveQuery($this->newModelQuery());