0
votes

I have few models generated by yii component gii. Now I want to do automatically inserting author, created and updated time. In Yii Cookbook it's present. First variant:


    public function behaviors()
    {
    return array(
        'timestamps' => array(
            'class' => 'zii.behaviors.CTimestampBehavior',
            'createAttribute' => 'created_on',
            'updateAttribute' => 'modified_on',
            'setUpdateOnCreate' => true,
            ),
        );
    }

And here are the second variant:


    protected function beforeValidate()
    {
    if($this->getIsNewRecord())
        $this->created_on = time();
    $this->modified_on = time();
    return true;
    }

In my situation I should create class which will replaced beforeSave method in every model which extends it. Is it wrong?

1
you need to show your work here. No one has much free time to checkout the book that you mentioned. So, edit your question.Idrees Khan

1 Answers

0
votes

You need to implement that in a behaviour and attach that behaviour to every class you want this to happen.

Use of Component Behavior

A component supports the mixin pattern and can be attached with one or several behaviors. A behavior is an object whose methods can be 'inherited' by its attached component through the means of collecting functionality instead of specialization (i.e., normal class inheritance). A component can be attached with several behaviors and thus achieve 'multiple inheritance'.

Behavior classes must implement the IBehavior interface. Most behaviors can extend from the CBehavior base class. If a behavior needs to be attached to a model, it may also extend from CModelBehavior or CActiveRecordBehavior which implements additional features specifc for models.

To use a behavior, it must be attached to a component first by calling the behavior's attach() method. Then we can call a behavior method via the component:

// $name uniquely identifies the behavior in the component
$component->attachBehavior($name,$behavior);
// test() is a method of $behavior
$component->test();

An attached behavior can be accessed like a normal property of the component. For example, if a behavior named tree is attached to a component, we can obtain the reference to this behavior object using:

$behavior=$component->tree;
// equivalent to the following:
// $behavior=$component->asa('tree');

A behavior can be temporarily disabled so that its methods are not available via the component. For example,

$component->disableBehavior($name);
// the following statement will throw an exception
$component->test();
$component->enableBehavior($name);
// it works now
$component->test();

It is possible that two behaviors attached to the same component have methods of the same name. In this case, the method of the first attached behavior will take precedence.

When used together with events, behaviors are even more powerful. A behavior, when being attached to a component, can attach some of its methods to some events of the component. By doing so, the behavior gets a chance to observe or change the normal execution flow of the component.

A behavior's properties can also be accessed via the component it is attached to. The properties include both the public member variables and the properties defined via getters and/or setters of the behavior. For example, if a behavior has a property named xyz and the behavior is attached to a component $a. Then we can use the expression $a->xyz to access the behavior's property.

More reading:
http://www.yiiframework.com/wiki/44/behaviors-events
http://www.ramirezcobos.com/2010/11/19/how-to-create-a-yii-behavior/