Yii Framework Forum: Timestampbehavior Not Found. - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Timestampbehavior Not Found.

#1 User is offline   dubby 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 171
  • Joined: 10-February 09
  • Location:Melbourne, Australia

Posted 28 February 2014 - 08:52 PM

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?
0

#2 User is offline   evercode 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 151
  • Joined: 23-December 12

Posted 02 March 2014 - 01:47 AM

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:

This post has been edited by evercode: 03 March 2014 - 04:25 PM

0

#3 User is offline   dubby 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 171
  • Joined: 10-February 09
  • Location:Melbourne, Australia

Posted 04 March 2014 - 05:43 AM

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

0

#4 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,907
  • Joined: 04-October 08
  • Location:DC, USA

Posted 04 March 2014 - 07:30 AM

The class is here: https://github.com/y...ampBehavior.php
Make sure you are using the latest version, not alpha version.
0

#5 User is offline   dubby 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 171
  • Joined: 10-February 09
  • Location:Melbourne, Australia

Posted 04 March 2014 - 07:52 AM

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?
0

#6 User is offline   zelenin 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 31
  • Joined: 24-May 13

Posted 04 March 2014 - 10:25 AM

View Postdubby, on 04 March 2014 - 07:52 AM, said:

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": "*",
0

#7 User is offline   dubby 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 171
  • Joined: 10-February 09
  • Location:Melbourne, Australia

Posted 06 March 2014 - 02:36 AM

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": "*"
        },

0

#8 User is offline   Suralc 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 48
  • Joined: 10-January 12
  • Location:Germany

Posted 06 March 2014 - 02:04 PM

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

#9 User is offline   dubby 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 171
  • Joined: 10-February 09
  • Location:Melbourne, Australia

Posted 07 March 2014 - 05:48 AM

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.
0

#10 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,703
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 07 March 2014 - 06:01 AM

View Postdubby, on 07 March 2014 - 05:48 AM, said:

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.
God is real unless declared as integer
0

#11 User is offline   dubby 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 171
  • Joined: 10-February 09
  • Location:Melbourne, Australia

Posted 08 March 2014 - 09:53 PM

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.
0

#12 User is offline   evercode 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 151
  • Joined: 23-December 12

Posted 09 March 2014 - 07:16 PM

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

public function behaviors()
{
return [
'timestamp' => [
'class' => 'yii\behaviors\TimestampBehavior',
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
'value' => 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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users