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

WIP: behaviors refactoring #2360

Closed
wants to merge 1 commit into from
Closed

WIP: behaviors refactoring #2360

wants to merge 1 commit into from

Conversation

lucianobaraglia
Copy link
Contributor

See #2269 and https://gist.github.com/lucianobaraglia/8d184cd9b9c7505a0b24

Usage example:

public function behaviors()
{
    return [
        // arbitrary attributes and values
        'attributestamp' => [
            'class' => 'yii\behaviors\AttributeStampBehavior',
            'attributes' => [
                \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => [
                    'expires_at' => function() {
                        return date('Y-m-d H:i:s', strtotime("+30 days"));
                    },
                    'created_by_name' => isset(\Yii::$app->user->identity) ? \Yii::$app->user->identity->username : 'n/a',
                ],
            ],
        ],
        // default implementation, sets `created_at` and `updated_at` with `date('Y-md H:i:s')`
        // also, `format` could be configured here
        'timestamp' => [
            'class' => 'yii\behaviors\TimestampBehavior',
            'format' => 'Y-m-d',
        ],
        // this will fill `created_by` and `author_id` with the behaviors default value on insert
        // and `updated_by` on update
        'blameable' => [
            'class' => 'yii\behaviors\BlameableBehavior',
            'attributes' => [
                \yii\db\ActiveRecord::EVENT_BEFORE_INSERT => [
                    'created_by',
                    'author_id',
                ],
                \yii\db\ActiveRecord::EVENT_BEFORE_UPDATE => [
                    'updated_by',
                ],
            ],              
        ],
    ];
}

$model = $this->owner;
$attributes = $this->attributes[$event->name];

if ($attributes !== null) {
Copy link
Member

Choose a reason for hiding this comment

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

Why is this check needed? Probably use the following code:

if (!empty($this->attributes[$event->name])) {
    foreach ($this->attributes[$event->name] as $key => $val) { ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Right...I just have the habit of give that kind of properties a shorter name...
Will wait for more comments from you to refactor...

Copy link
Member

Choose a reason for hiding this comment

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

The check above also ensures attributes has the key $event->name, even though we are almost certain.

@qiangxue
Copy link
Member

qiangxue commented Feb 7, 2014

Ok, I'm done with the code review. Thanks!

/**
* AttributeStampBehavior is used to fill an ActiveRecord's object attributes with arbitrary data when specified events occurs.
*
* AttributeStampBehavior is mainly used as a base class for other behaviors, like `TimestampBehavior` and `BlameableBehavior`.
Copy link
Member

Choose a reason for hiding this comment

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

AttributeStampBehavior is mainly → It is mainly

@lucianobaraglia
Copy link
Contributor Author

Well...after review I don't think if this could be helpful.
You are free to take some of it or not and make the changes you think...
If this is merged, please remember that original TimestampBehavior was a work of @creocoder...

Let me know if I can help in anything... 😃

@qiangxue
Copy link
Member

qiangxue commented Feb 7, 2014

after review I don't think if this could be helpful.

Do you mean the three layer thing?

@lucianobaraglia
Copy link
Contributor Author

after review I don't think if this could be helpful.

Do you mean the three layer thing?

I mean the entire refactor...
Is this bringing some benefits?

@qiangxue
Copy link
Member

qiangxue commented Feb 7, 2014

Sorry if my code review made you feel frustrated. I think the refactoring still makes sense. I will come back to this later when I get time.

@lucianobaraglia
Copy link
Contributor Author

@qiangxue never ever, I am not that kind of developer!!! 😃
I appreciate your time!
For me, having my code or ideas taken in a professional framework is still a weird idea! So no problem, I am learning a LOT just having you guys reviewing my code...
But please, at least don't post it in http://thedailywtf.com/Series/CodeSOD.aspx 😐

@samdark
Copy link
Member

samdark commented Feb 7, 2014

@lucianobaraglia your code isn't bad. We just suggest how to make it better.

@qiangxue
Copy link
Member

qiangxue commented Feb 7, 2014

Agree with @samdark.

BTW, personally I don't quite like the three layers thing because it's a bit over designed and may confuse users without very good explanation and understanding.

@lucianobaraglia
Copy link
Contributor Author

@samdark I know...thanks!
@qiangxue is the only way I came with that allows the AttributeStampBehavior give that freedom.
The child classes are only pre-configured types of AttributeStampBehavior, that have it's own default value (and don't allow setting anything else).
I guess -one of the- problem is in stampAttribute in child classes, where I unnecesary check for values that won't be used.

@Ragazzo
Copy link
Contributor

Ragazzo commented Feb 19, 2014

ok, so if we have this behavior, should not we have then behavior for is_deleted attribute for example? would like to.

@lucianobaraglia
Copy link
Contributor Author

@Ragazzo with this approach several automatic attributes stamp could be implemented easily extending AttributeStampBehavior...
I think in VersionableBehavior, for example... and other more specifics...like SoftDeleteBehavior, etc...

@Ragazzo
Copy link
Contributor

Ragazzo commented Feb 19, 2014

yes, but if so, then maybe they should go to yii2-behaviors extension.

@lucianobaraglia
Copy link
Contributor Author

@Ragazzo would be nice!
It would be great if you can read all the discussion and help with the @qiangxue and @samdark ideas about this all (and I will learn from all of you 😃 )!

@Ragazzo
Copy link
Contributor

Ragazzo commented Feb 19, 2014

i am fine with their suggestions, however some general behaviors are good, and soft-delete is one of them. the question is that should that be yii2-behaviors extension or not.

@qiangxue
Copy link
Member

I don't think we need to put them in an extension. We usually create an extension if it has some external dependencies. Otherwise, separating them in an extension only brings trouble to users. The benefit of reducing code size is negligible.

@Ragazzo
Copy link
Contributor

Ragazzo commented Feb 19, 2014

ok, so what is for soft-delete behavior? it is needed same time as other for updating time and blameable behavior.

@qiangxue
Copy link
Member

Yes, it's good to have.

@Ragazzo
Copy link
Contributor

Ragazzo commented Feb 19, 2014

ok, @lucianobaraglia can you add behavior for soft-delete? thanks)

@lucianobaraglia
Copy link
Contributor Author

@Ragazzo this PR was just a refactoring proposal that's still far to be implemented beacuse code has to be improved, I think...
BTW, just for reference, the list of behaviors that comes to my mind:

  • AttributeStampBehavior
  • TimestampBehavior
  • BlameableBehavior
  • VersionableBehaior
  • SoftDeleteBehavior

Some other...?

@Ragazzo
Copy link
Contributor

Ragazzo commented Feb 19, 2014

first two are not the same? and for what is version...?

@qiangxue
Copy link
Member

What about TranslatableBehavior?

@lucianobaraglia
Copy link
Contributor Author

AttributeStampBehavior is for custom attributes and data stamp...see example here in the first comment...
And Versionable is just taken from Doctrine Behaviors, and works adding a version number each time a record is changed...

@lucianobaraglia
Copy link
Contributor Author

@qiangxue it would be amazing, but I think maybe it's out of the scope of this refactoring idea...

@Ragazzo
Copy link
Contributor

Ragazzo commented Feb 19, 2014

Translatable? yes, better finish this PR as @lucianobaraglia said. @lucianobaraglia can you open then separated issue with desired behaviors list?

@qiangxue qiangxue closed this in 02d5b20 Feb 20, 2014
@cebe cebe modified the milestones: 2.0 GA, 2.0 Beta Feb 20, 2014
cebe added a commit that referenced this pull request Feb 20, 2014
* 'master' of github.com:yiisoft/yii2:
  Fixes #2360: Added `AttributeBehavior` and `BlameableBehavior`, and renamed `AutoTimestamp` to `TimestampBehavior`
@lucianobaraglia lucianobaraglia deleted the wip-behaviors-refactoring branch February 20, 2014 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants