mercredi 3 mai 2017

How to write an elegant behaviors in Model of Yii2?

When I write model's method behaviors. There are some behaviors defined in the returned value. Look at the following code:

'timestamp' => [
    'class' => 'yii\behaviors\TimestampBehavior',
    'attributes' => [
        ActiveRecord::EVENT_BEFORE_INSERT => ['submitTime'],
    ],

it means that the attribute submitTime will generate automatically. I want to judge whether the attribute submitTime is null. it will generate when it is null. I achieve like this:

'value' => function ($event) {
    if ($this->submitTime) {
        return $this->submitTime;
    }
    return time();
},

Is there an elegant way to achieve my idea?




Aucun commentaire:

Enregistrer un commentaire