-
-
Notifications
You must be signed in to change notification settings - Fork 6.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
Enhance yii\behaviors\AttributeBehavior
#11969
Conversation
`AttributeBehavior` can preserve current attribute values now.
Hmm... this makes https://github.com/yiisoft/yii2/blob/master/framework/behaviors/SluggableBehavior.php#L88 pointless so either @Kolyunya what do you think would be the best? |
@samdark I'm not sure but I think it is important to maintain backward compatibility. If it is, we should choose the first option. Flag names ( |
Both my suggestions are backwards compatible, aren't they? |
@samdark yes, they are. I just thought that deprecated properties and methods are subject for removal. |
In 2.1 — yes. |
Thinking about it more, condition for not touching a field could be anything: only certain existing value, value of another field etc. |
Since closures are supported for values and we have dirty attributes support, we can achieve it using existing code: 'value' => function($event) {
if (empty($this->create_time)) {
return time();
}
return $this->create_time;
} |
So I'd decline this pull request and document ↑ |
@samdark I'm sorry but we can't achieve this using existing code in a general scenario when we are populating multiple attributes via single behavior. Use-case: Page entity has three attributes: meta_title, meta_description, meta_keywords. We need to copy title to each of the attributes only if they are empty. This will allow users to set some of them manually and to let the behavior fill the others. The only workaround is to add three behaviors for each attribute with custom value which will check if the corresponding attribute is empty or not which is quite inelegant. |
|
Right, it's not convenient, at least... |
just want to write about it, i want use BlameableBehavior with beforeValidate without rewrite filled user_id |
how about a |
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.
Changelog line needed.
@@ -86,6 +86,11 @@ class AttributeBehavior extends Behavior | |||
* @since 2.0.8 | |||
*/ | |||
public $skipUpdateOnClean = true; | |||
/** | |||
* @var boolean whether to skip attribute update when it is not empty | |||
* @since 2.0.10 |
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.
2.0.12
AttributeBehavior
can preserve current attribute values now.Basic tests coverage is included.