Active record TimestampBehavior

I have an active record like so…


class Transaction extends \yii\db\ActiveRecord

{

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'transaction';

    }

    

    

    /**

     * @inheritdoc

     */

    public function behaviors()

    {

        return [

            TimestampBehavior::className(),

            'timestamp' => [

                'class' => 'yii\behaviors\TimestampBehavior',

                'attributes' => [

                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],

                    //ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],

                ],

            ],

        ];

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['user_id', 'request', 'response', 'order_id'], 'required'],

            [['amount'], 'number'],

            [['user_id', 'order_id'], 'integer'],

            [['created_at', 'transaction_number', 'reference_number', 'status'], 'safe'],

            [['request', 'response'], 'string', 'max' => 2000],

            [['comments'], 'string', 'max' => 255]

        ];

    }

    

 


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return [

            'id' => 'ID',

            'user_id' => 'User ID',

            'amount' => 'Amount',

            'request' => 'Request',

            'response' => 'Response',

            'comments' => 'Comments',

            'created_at' => 'Created At',

            'transaction_number' => 'Transaction Number',

            'status'=>'Status'

        ];

    }...

As you can see, I wan’t a “created_at” behavior without the “updated_at” behavior. However, if I add one it forces me to add the other even though the updated_at behavior is not set. I get an error saying, Setting unknown property: common\models\ar\Transaction::updated_at.

I don’t understand why it is saying this because I removed the updated_at behavior. Am I not allowed to add one without the other?

The following code is from API doc.

http://www.yiiframework.com/doc-2.0/yii-behaviors-timestampbehavior.html

And you can set false to ‘updateAtAttribute’.

http://www.yiiframework.com/doc-2.0/yii-behaviors-timestampbehavior.html#$updatedAtAttribute-detail

Thanks for the replies. I changed my active record to use this…


    

    /**

     * @inheritdoc

     */

    public function behaviors()

    {

        return [

            TimestampBehavior::className(),

            'timestamp' => [

                'class' => 'yii\behaviors\TimestampBehavior',

                'attributes' => [

                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],

                    ActiveRecord::EVENT_BEFORE_UPDATE => false,

                ],

            ],

        ];

    }

but I still get the error message "Setting unknown property: common\models\ar\Transaction::updated_at".

when I try to use this…




public function behaviors()

{

    return [

        [

            'class' => TimestampBehavior::className(),

            'createdAtAttribute' => 'create_time',

            'updatedAtAttribute' => 'update_time',

            'value' => new Expression('NOW()'),

        ],

    ];

}



it says, "Class created_at does not exist".

Also, it might be worth mentioning that I have to create the “created_at” column int he database as int(11) because mysql timestamp won’t work with TimestampBehavior.

What about this?




public function behaviors()

{

    return [

        [

            'class' => TimestampBehavior::className(),

            'createdAtAttribute' => 'created_at',

            'updatedAtAttribute' => false,

            'value' => new Expression('NOW()'),

        ],

    ];

}



Sorry, that is what I mean to type in the last reply. That is how I did it but then get an error saying “Class created_at does not exist”. I don’t know why it’s looking at a “Class” of type “created_at”. IS it possible that this is a bug in the framework?

Also, if I set both of the attributes to false I get an error saying, "Unsupported configuration type: boolean".

I’m sorry, but I had overlooked the possible cause of the problem in your first post.

Now I think I got what was wrong.

In the above, two instances of the TimestampBehavior are registered. One with the default values, and the other with your special settings.

It had to be like this:




class Transaction extends \yii\db\ActiveRecord

{

    ...

    public function behaviors()

    {

        return [

            /* TimestampBehavior::className(), */

            'timestamp' => [

                'class' => 'yii\behaviors\TimestampBehavior',

                'attributes' => [

                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],

                    //ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],

                ],

            ],

        ];

    }

    ...



http://www.yiiframework.com/doc-2.0/guide-concept-behaviors.html#attaching-behaviors

Thank you Softark. This did the trick. Much appreciated.