Luciano Nascimento
Luciano Nascimento

Reputation: 2600

Yii disable Model Behavior for one Controller

Im using a behavior(DateTimeI18NBehavior) in Users.php model, but specifically in a controller (ApiController.php) I would like to disable it.


Model - Users.php:

public function behaviors()
{
    return array(
        'datetimeI18NBehavior'=>array(
            'class' => 'ext.DateTimeI18NBehavior',
        ),
    );
}

I know that I can it disable with:

$model->disableBehavior('datetimeI18NBehavior');

But how to disable to entire Controller?

Upvotes: 1

Views: 1801

Answers (1)

rinat.io
rinat.io

Reputation: 3198

Not sure, but maybe this would work:

class ApiController extends CController
{
    function init()
    {
        Users::model()->disableBehavior('datetimeI18NBehavior');
    }
}

Or you can try to add some logic in your model:

function behaviors()
{
    if (Yii::app()->controller->uniqueId != 'api') {
        return parent::behaviors();
    }
    return array(
        'datetimeI18NBehavior'=>array(
            'class' => 'ext.DateTimeI18NBehavior',
        ),
    );
}

Both ways aren't perfect though in my opinion.

Upvotes: 5

Related Questions