Posts Tagged with yii2 TimestampBehavior

Displaying 1-2 of 2 results.
Wiki: Yii2 behavior and beforeSave usage example without conflicts and errorswiki
posted by admin on February 22, 2017
Behaviors:

public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_dt',
'updatedAtAttribute' => 'updated_dt',
'value' => your datetime format value,//or remove row to default with UNIX TIMESTAMP
],
];
}

BeforeSave:

public function beforeSave($insert)
{
if (parent::beforeSave($insert)) {

if ($this->isNewRecord){
//to do
}else{
//to do
}

return true;
}
return false;
}
Read more
Resolved: Yii2 Class 'app\models\TimestampBehavior' not found
posted by admin on February 21, 2017
I have User model and have behavior in model

<?php

namespace app\models;

use Yii;
use yii\base\NotSupportedException;
use yii\db\ActiveRecord;
use yii\helpers\Security;
use yii\web\IdentityInterface;
use app\helpers\Table;
use app\helpers\App;
/**
* This is the model class for table "tbl_user".
*
* @property integer $id
* @property string $username
* @property string $password
* @property string $email
* @property integer $role_id
* @property integer $user_id
* @property integer $status_id
* @property integer $created_dt
* @property integer $updated_dt
* @property integer $timezone_id
*/
class User extends \yii\db\ActiveRecord implements IdentityInterface
{
public $authKey;
public $accessToken;

/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%'.Table::$user.'}}';
}

public function behaviors()
{
return [
'timestamp' => [
'class' => TimestampBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'creation_time',
ActiveRecord::EVENT_BEFORE_UPDATE => 'update_time',
],
'value' => function() { return App::datetime_now_unix(); },
],
];
}
. . .
}

Error

Class 'app\models\TimestampBehavior' not found
Read more