-
Notifications
You must be signed in to change notification settings - Fork 46
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
Refactor Model::getFields #691
base: develop
Are you sure you want to change the base?
Conversation
1132e47
to
945d6b9
Compare
I do not get this - we should get all and filter then, what is the advantage of your approach? |
This is also true. Main point was to introduce the 'persisting' preset to avoid duplicate code. |
d5de024
to
0d962c3
Compare
2d89841
to
40cbf73
Compare
c067309
to
66cb0b7
Compare
2c70758
to
9c0472a
Compare
src/Model.php
Outdated
@@ -625,15 +625,21 @@ public function isDirty(string $field): bool | |||
* | |||
* @return Field[] | |||
*/ | |||
public function getFields($filters = null): array | |||
public function getFields($filters = null, $onlyFields = null): array |
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.
bool $onlyFields
?
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.
Changed
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.
but why to null by default?
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.
To keep BC
$model->getFields()
returns all fields historically
$model->getFields('system')
returns system fields in onlyFields
So if $filter
argument set $onlyFields
defaults to true
1b67214
to
d6307b3
Compare
} elseif (is_string($filter)) { | ||
$filter = [$filter]; | ||
if ($filters === null) { | ||
return $onlyFields ? $this->getFields(self::FIELD_FILTER_ONLY_FIELDS) : $this->fields; |
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.
no need to recurse, just set $filter...
return array_filter($this->fields, function (Field $field, $name) use ($filter) { | ||
// do not return fields outside of "only_fields" scope | ||
if ($this->only_fields && !in_array($name, $this->only_fields, true)) { | ||
$onlyFields = $onlyFields ?? true; |
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.
remove this then
return $field->isEditable(); | ||
case self::FIELD_FILTER_VISIBLE: | ||
return $field->isVisible(); | ||
case self::FIELD_FILTER_ONLY_FIELDS: |
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 should not need FIELD_FILTER_ONLY_FIELDS
then
I have another idea: We can introduce multiple arguments which will be filters merged with AND. So It will not be BC though I believe. |
I like minimalistic APIs. We extend Model more and more for BC... And the questions is what this more and more code provides? Does it really help and reduce the maintanence requirements? If yes, go ahead! |
fbff6c0
to
d64c4c0
Compare
d64c4c0
to
482fa01
Compare
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 like to deduplicate the code.
However, I am not sure if we should mix very unrelated attributes together and offer partial "not" constants
1e313dc
to
b413fd1
Compare
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.
Good improvement.
Do address feedback from @mvorisek
TODO rebase on develop, it is based on #690, only the last 10 commits are relevant (others are from the mentioned PR) - be3cb50...d6307b3 |
…sting' filter preset
d6307b3
to
01375df
Compare
Model::FIELD_FILTER_PERSIST
filter presetModel::FIELD_FILTER_ONLY_FIELDS
filter preset