Timestampbehavior Not Found.

I am creating a test project with Yii2 and trying to use the timestamp behavior but having a hard time implementing it. I have created the model and CRUD for the table market and have attempted to add a behavior to models/Market.php as follows:


<?php


namespace app\models;


use yii\behaviors\TimestampBehavior;


class Market extends \yii\db\ActiveRecord

{


    public function behaviors()

    {

        return [

            'timestamp' => [

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

                'attributes' => [

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

                    ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_at',

                ],

            ],

        ];

    }



However, when loading the page I get the error


Class 'yii\behaviors\TimestampBehavior' not found

I’ve searched the whole source code including vendor/yiisoft but there is no class of TimestempBehavior anywhere. Do I need to include/install it some other way first?

When I’m using TimestampBehavior on the user model, I don’t need to specify:

use yii\behaviors\TimestampBehavior;

So maybe that is the problem. I was having a different problem with it. The default behavior is to set an integer and I wanted a time stamp. In that case, I had to make 2 changes:

  1. use yii\db\Expression;

  2. add the following:

‘value’ => new Expression(‘NOW()’),

The docs for this are here:

Thanks for the response. I tried it but I’m still getting the same error:




Class 'yii\behaviors\TimestampBehavior' not found



Edit: I forgot to answer the other part. If I exclude the use statement for TimestampBehavior I get this error:




Class 'app\models\TimestampBehavior' not found



The class is here: https://github.com/yiisoft/yii2/blob/master/framework/behaviors/TimestampBehavior.php

Make sure you are using the latest version, not alpha version.

Thanks for the reply qiang,

I initially installed Yii2 using Composer and I can see that it is alpha. Generally with Composer I would type ‘composer update’ to update but this doesn’t change anything. How would I go about upgrading to a newer version?

"yiisoft/yii2": "*",

My composer.json in regards to Yii looks like this.




        "minimum-stability": "alpha",

        "require": {

                "php": ">=5.4.0",

                "yiisoft/yii2": "*",

                "yiisoft/yii2-swiftmailer": "*",

                "yiisoft/yii2-bootstrap": "*",

                "yiisoft/yii2-debug": "*",

                "yiisoft/yii2-gii": "*"

        },



Change minimum stability to dev or add @dev after each yii-package version.

Thanks all for your responses.

I tried Suralc’s last suggestion by setting the Yii packages to @dev but reloading the page gave me the following error:




Undefined class constant 'EVENT_AFTER_SEND'



I’m in heavy development on a project (for fun, not production) so I’m skipping past this problem for now.

EVENT_AFTER_SEND is used in BaseMailer, not in TimestampBehavior.

Maybe something is wrong with your code.

I don’t know. I just did a clean install of Yii2 using Composer:




curl -sS https://getcomposer.org/installer | php



And the site (front page at least) loaded fine. I then updated composer.json to the following:




        "require": {

                "php": ">=5.4.0",

                "yiisoft/yii2": "@dev",

                "yiisoft/yii2-swiftmailer": "@dev",

                "yiisoft/yii2-bootstrap": "@dev",

                "yiisoft/yii2-debug": "@dev",

                "yiisoft/yii2-gii": "@dev"

        },




and ran composer update

But I get this error again:




Undefined class constant 'EVENT_AFTER_SEND'



It can’t be my code this time as I haven’t added any. I must be installing and upgrading incorrectly.

I’m using dev version. My Timestamp behavior works without the namespace but calling the class with full path:

public function behaviors()

{


	return [


		'timestamp' =&gt; [


			'class' =&gt; 'yii&#092;behaviors&#092;TimestampBehavior',


			'attributes' =&gt; [


				ActiveRecord::EVENT_BEFORE_INSERT =&gt; ['created_at', 'updated_at'],


				ActiveRecord::EVENT_BEFORE_UPDATE =&gt; ['updated_at'],


			],


            'value' =&gt; new Expression('NOW()'),


		],


	];


}

As I mentioned earlier, to use Expression, you have to put this at the top of the file:

use yii\db\Expression;

Hope that helps.