AR TimestampBehavior without "updated_at" attribute


(James Luke318) #1

I have some models that are only created and never updated. They just have a "created_at" timestamp. However, when I implement the TimestampBehavior I get errors thrown if there is no "EVENT_BEFORE_UPDATE" action set.

I want to be able to set the created_at attribute using the TimestampBehavior without having to add an "updated_at" attribute.

I have tried the following but it did not work…




    public function behaviors()

    {

        return [

            TimestampBehavior::className(),

            'timestamp' => [

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

                'attributes' => [

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

                    ActiveRecord::EVENT_BEFORE_UPDATE => false,

                ],

            ],

        ];

    }



Any idea how to achieve this?


(Softark) #2

Try either




    public function behaviors()

    {

        return [

            'timestamp' => [

                TimestampBehavior::className(),

                'attributes' => [

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

                    ActiveRecord::EVENT_BEFORE_UPDATE => false,

                ],

            ],

        ];

    }



or




    public function behaviors()

    {

        return [

            'timestamp' => [

                TimestampBehavior::className(),

                'createdAtAttribute' => 'created_at'

                'updatedAtAttribute' => false,

             ],

        ];

    }



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


(James Luke318) #3

Thanks for the shot Softark but I couldn’t get either of these ways, or any combination of them to work.

I tried this…


    

    public function behaviors()

    {

        return [

            TimestampBehavior::className(),

            'timestamp' => [

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

                'attributes' => [

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

                    ActiveRecord::EVENT_BEFORE_UPDATE => false,

                ],

            ],

        ];

    }



and,




    public function behaviors()

    {

        return [

            TimestampBehavior::className(),

            'timestamp' => [

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

                'attributes' => [

                    'createdAtAttribute' => 'created_at',

                    'updatedAtAttribute' => false,

                ],

            ],

        ];

    }



They both still threw an error saying "Setting unknown property: common\models\UserAccountCredit::updated_at"


(Pawel) #4

This should be




public function behaviors()

{

    return [

        'timestamp'  => [

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

            'updatedAtAttribute' => false,

        ],

    ];

}




(Softark) #5

    

    public function behaviors()

    {

        return [

            /* TimestampBehavior::className(),  ... This line should be omitted */

            'timestamp' => [

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

                'attributes' => [

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

                    ActiveRecord::EVENT_BEFORE_UPDATE => false,

                ],

            ],

        ];

    }



or




    public function behaviors()

    {

        return [

            /* TimestampBehavior::className(),  ... This line should be omitted */

            'timestamp' => [

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

                'attributes' => [

                    'createdAtAttribute' => 'created_at',

                    'updatedAtAttribute' => false,

                ],

            ],

        ];

    }




(James Luke318) #6



public function behaviors()

{

    return [

        'timestamp'  => [

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

            'updatedAtAttribute' => false,

        ],

    ];

}



I tried this but I’m still getting the same error. It’s weird because I swear I have achieved this before in an old AR model that is now deleted. I looked through the GitHub history but couldn’t find it.

I also tried this, but no success…




public function behaviors()

{

    return [

        'timestamp'  => [

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

            'updatedAtAttribute' => false,

                'attributes' => [

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

                    ActiveRecord::EVENT_BEFORE_UPDATE => false,

                ],

        ],

    ];

}




(Renkas) #7

Try this. If you set attributes in config then the default attributes that include updated_at should not be set at all - so there is nothing for you to unset.




public function behaviors()

{

    return [

        'timestamp'  => [

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

            'attributes' => [

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

            ],

        ],

    ];

}




(Manojkiran) #8

Try this out

public function behaviors()
    {
        return [
            [
                'class' => TimestampBehavior::className(),
                
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
                // if you're using datetime instead of UNIX timestamp:
                'value' => new Expression('NOW()'),
            ],
        ];
    }