-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Updated the CI model for improved/consistent delete and update use. #914
Conversation
} | ||
|
||
// you can not proceed. | ||
if ($id === null && $data === null) return false; |
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'd prefer to throw an exception here. It seems like any time this would happen would be something the developer should have caught so we should fail loudly so they have a chance to fix it. Just returning false here would make the bug harder to find and easier to slip through into production.
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.
Yeah, that's a good idea, otherwise, it's a silent death with no reason why.
// because we're going to use "where" for the items to updated | ||
// id became a surrogate for data argument | ||
// not ideal, but lets see where this goes... | ||
if ($data === null) |
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.
The more I think about the polymorphic aspects of our parameters the less I like it. I know we all had a discussion about this previously and decided to go this route, but splitting it into two methods would be more clear. Maybe updateWith($data)
and updateById($id, $data)
or something?
What are your thoughts?
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 do agree it's not the best solution, I'd really like it to be one method for simplicity sake. However, if we think it's doing too much, we could branch it out. I might be in favor of doing something like updateWith
since the consistency with delete uses id as its argument. Though we might want to make it (sort of back to the original idea of) updateWhere
and deleteWhere
keeping the update/delete methods for only primary keys use.
What makes you say the ModelTest is messy? |
@lonnieezell Sorry I meant messy in a broad sense not specifically to the ModelTest, I have to rework my local environment. |
@lonnieezell Once I do that, then I'll put up the proper tests and we can work to merge this in. |
Being picky, but we expect commits to be GPG-signed :) |
{ | ||
$this->trigger('beforeDelete', ['id' => $id, 'purge' => $purge]); | ||
if ($id !== null && !is_array($id)) $id = [$id]; |
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 to remove the beforeDelete
trigger?
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.
That is not intentional, and it should be left in there.
PR Usage:
Proposed changesWhat do you think about to work like the Query Builder updateBatch() in the Model $data = array(
array(
'id' => '1' ,
'title' => 'My title' ,
'name' => 'My Name 2' ,
'date' => 'My date 2'
),
array(
'id' => '2' ,
'title' => 'Another title' ,
'name' => 'Another Name 2' ,
'date' => 'Another date 2'
)
);
// Query Builder way
$builder->updateBatch($data, 'id');
// Model way
$model->update($data); Then, would be necessary to check if the first param is array. If is not array, then is the primaryKey: If is array, we have two use cases:
To know if he wants a updateBatch, we could to check the value of the first key of If it is an array, then call MethodThe method would be like: /*
* @param int|string|array $id Primary Key or an array of update values.
* If is an associative array will be called an update batch.
* @param array|object $data
*
* @return bool|int True on success, false on fail
* or the number of affected rows as integer if using update batch
*/
public function update($id, $data = null) Usage:
$model->update(1, ['color' => 'blue']);
$model->where(['id' => 1])->update(['color' => 'blue']);
$model->update([
['id' => 1, 'color' => 'blue'],
['id' => 2, 'color' => 'red'],
['id' => 3, 'color' => 'yellow'],
]); * Something similar could be implemented for the |
@natanfelles I can't go with that. Too many overriding uses of the same method. We're already pushing it on these, honestly. :) |
@lonnieezell Ok. Thanks! I just looked at the PR and thought I could help with a guess. 👍 |
Sorry for the urge, but I still think this $model->update([1, 2, 3], ['color' => 'blue']);
// UPDATE `{$this->table}` SET `color` = 'blue' WHERE `id` IN ('1', '2', '3') Does the same that the current PR update multiple and my proposed usage 2: $model->whereIn('id', [1, 2, 3])->update(['color' => 'blue']);
// UPDATE `{$this->table}` SET `color` = 'blue' WHERE `id` IN ('1', '2', '3') Then let the method pick up the $model->update([
['id' => 1, 'color' => ''],
['id' => 2, 'color' => 'red'],
['id' => 3, 'color' => 'yellow'],
]);
/*
UPDATE `{$this->table}` SET `color` = CASE
WHEN `id` = '1' THEN ''
WHEN `id` = '2' THEN 'red'
WHEN `id` = '3' THEN 'yellow'
ELSE `color` END
WHERE `id` IN ('1','2','3')
*/ I returned to this because I was inserting/updating several items in a single query, through the *Batch() methods, and I saw that the validation did not run. So I came back to find this concept more interesting, because we can already do the validation array by array and stop the process if necessary. * Array or Object But if it is still too many overriding uses of the method, what will be done in relation to the *Batch() methods? I believe that each item should be validated in the Model, since the validation is already prepared in it.
|
Changes never pushed. I'm pushing these changes in a separate commit later tonight. |
Sorry, this took a while, but here is the tested version of the new model updates for
update()
anddelete()
methods.Usage:
Removed Methods:
findWhere()
deleteWhere()
Referencing:
#865
Any feedback is greatly appreciated. I've tested these myself, and haven't gone into the phpunit test yet due to the mess it currently is. (initial post for this update)