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

Documentation's behavior needs clarification #4845

Closed
tebazil opened this issue Aug 28, 2014 · 6 comments
Closed

Documentation's behavior needs clarification #4845

tebazil opened this issue Aug 28, 2014 · 6 comments
Labels
type:docs Documentation

Comments

@tebazil
Copy link
Contributor

tebazil commented Aug 28, 2014

In the documentation: https://github.com/yiisoft/yii2/blob/master/docs/guide/concept-behaviors.md#handling-component-events
This example

namespace app\components;

use yii\db\ActiveRecord;
use yii\base\Behavior;

class MyBehavior extends Behavior
{
    // ...

    public function events()
    {
        return [
            ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate',
        ];
    }

    public function beforeValidate($event)
    {
        // ...
    }
}

is questionable, since, if I get behavior logic right, beforeValidate($event) should be triggered, as it is default handler for the ActiveRecord::EVENT_BEFORE_VALIDATE.

If I am right, we should expand on "default" handlers in text and add some custom name for a handler method, as it is rather confusing.

If I am understanding the mechanism wrong, than the issue is invalid and issue should be closed. In this case, I ask somebody of the contributors to answer my design questions in the forum:

Thanks.

@qiangxue
Copy link
Member

There's no such thing as default event handler. Where did you get this understanding from?

@tebazil
Copy link
Contributor Author

tebazil commented Aug 28, 2014

@qiangxue ,
In Yii 1.1, there was CActiveRecordBehavior, which defined typical AR events and their handlers itself. Most of the time we had no need to override it.

class CActiveRecordBehavior extends CModelBehavior
{
...
public function events()
{
return array_merge(parent::events(), array(
'onBeforeSave'=>'beforeSave',
'onAfterSave'=>'afterSave',
'onBeforeDelete'=>'beforeDelete',
'onAfterDelete'=>'afterDelete',
'onBeforeFind'=>'beforeFind',
'onAfterFind'=>'afterFind',
'onBeforeCount'=>'beforeCount',
));
}
....

So I thought it's a typical approach throughout the system - to provide default method handler names.
I was wrong.
The issue is invalid for those who know the architecture of the framework.

However, from the user's point of view, who is migrating from v1 to v2, the issue remains.

We are used to extend from CActiveRecordBehavior and to provide no events() ourselves. Now there is no such a behavior, we are extending from Behavior and need to provide events().
This might be confusing for the users, don't you think?

@qiangxue
Copy link
Member

The events() syntax is still the same. Neither 1.1 nor 2.0 has the such default event handler thing.

The only thing changed is that we dropped ActiveRecordBehavior in 2.0. So if you want to write a AR behavior and respond to some of its events, you have to do this yourself now by writing events().

@tebazil
Copy link
Contributor Author

tebazil commented Aug 28, 2014

So if you want to write a AR behavior and respond to some of its events, you have to do this yourself now by writing events().

I think this should be in the documentation to prevent confusion. Mainly that was what the issue is about.

@tebazil
Copy link
Contributor Author

tebazil commented Aug 28, 2014

Another argument to support this is that most of the behaviors written seem to be extending CActiveRecordBehavior.

@qiangxue
Copy link
Member

Added the doc as suggested. Thanks.

qiangxue added a commit that referenced this issue Aug 28, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:docs Documentation
Projects
None yet
Development

No branches or pull requests

3 participants