Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Enhance yii\behaviors\AttributeBehavior #11969

wants to merge 1 commit into from

Conversation

Kolyunya
Copy link
Contributor

@Kolyunya Kolyunya commented Jul 17, 2016

Q A
Is bugfix? no
New feature? yes
Breaks BC? no
Tests pass? yes
Fixed issues #11923

AttributeBehavior can preserve current attribute values now.
Basic tests coverage is included.

`AttributeBehavior` can preserve current attribute values now.
@samdark
Copy link
Member

samdark commented Jul 17, 2016

Hmm... this makes https://github.com/yiisoft/yii2/blob/master/framework/behaviors/SluggableBehavior.php#L88 pointless so either immutable should be moved into base AttributeBehavior or immutable should be deprecated.

@Kolyunya what do you think would be the best?

@samdark samdark self-assigned this Jul 17, 2016
@Kolyunya
Copy link
Contributor Author

Kolyunya commented Jul 18, 2016

@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 (skipUpdateOnClean and immutable) won't be consistent though.

@samdark
Copy link
Member

samdark commented Jul 18, 2016

Both my suggestions are backwards compatible, aren't they?

@Kolyunya
Copy link
Contributor Author

@samdark yes, they are. I just thought that deprecated properties and methods are subject for removal.

@samdark
Copy link
Member

samdark commented Jul 18, 2016

In 2.1 — yes.

@samdark samdark added this to the 2.0.10 milestone Jul 18, 2016
@samdark
Copy link
Member

samdark commented Jul 18, 2016

Thinking about it more, condition for not touching a field could be anything: only certain existing value, value of another field etc.

@samdark
Copy link
Member

samdark commented Jul 18, 2016

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;
}

@samdark
Copy link
Member

samdark commented Jul 18, 2016

So I'd decline this pull request and document ↑

@Kolyunya
Copy link
Contributor Author

Kolyunya commented Jul 18, 2016

@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.

@samdark
Copy link
Member

samdark commented Jul 18, 2016

$skipUpdateOnFilled is per-behavior switch. Code above is the same.

@samdark
Copy link
Member

samdark commented Jul 18, 2016

Right, it's not convenient, at least...

@samdark samdark added type:enhancement and removed type:docs Documentation labels Jul 18, 2016
@DrDeath72
Copy link
Contributor

just want to write about it, i want use BlameableBehavior with beforeValidate without rewrite filled user_id

@Faryshta
Copy link
Contributor

Faryshta commented Sep 8, 2016

how about a $when callable that can return false if you don't want the attribute to update?

@SilverFire SilverFire modified the milestones: 2.0.11, 2.0.10 Sep 10, 2016
@samdark samdark modified the milestones: 2.0.12, 2.0.11 Dec 15, 2016
@samdark samdark removed their assignment Dec 15, 2016
@Kolyunya
Copy link
Contributor Author

@samdark @cebe what is the status of this PR? Is it under discussion or does it need some modifications?

Copy link
Member

@samdark samdark left a 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2.0.12

@Kolyunya
Copy link
Contributor Author

@samdark I wasn't able to push to this PR (since I already deleted that branch and repo) and I made a new one.

@Kolyunya Kolyunya closed this Feb 15, 2017
@cebe cebe removed this from the 2.0.12 milestone Feb 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants