[solved] blameable behavior's $defaultValue puts null into insert

Hello.

I’ve got problem with BlameableBehaviour’s variable $defaultValue. I have created project with Yii’s advanced template, then wanted to use mentioned behavior in console app like that:




use yii\behaviors\BlameableBehavior;

use yii\behaviors\TimestampBehavior;

//[..]

class Article extends \yii\db\ActiveRecord

//[..]

public function behaviors()

    {

        return [

            [

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

                // desired configuration, doesn't work

                'defaultValue' => Yii::$app->params['dbSystemUserId']

                

                // test configuration, doesn't work

                // 'defaultValue' => '1',


                // another test configuration: working!

                //'value' = '1'

            ],

            [   

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

            ]


        ];

    }

Use MySQL database created by migration:




//[..]

public function safeUp()

    {

        $this->createTable('article', [

            'id' => $this->primaryKey(),

            'shop_id' => $this->integer()->notNull(),

            'name' => $this->string()->notNull(),

            'netPchPrc' => $this->money(),

            'groSalPrc' => $this->money(),

            'vat' => $this->decimal(4,2),

            'created_by' => $this->integer()->notNull(),

            'created_at' => $this->timestamp()->notNull(),

            'updated_by' => $this->integer()->notNull(),

            'updated_at' => $this->timestamp()->notNull(),

        ]);

//[..]



Then an attempt to save record ends with error “SQLSTATE[23000]: Integrity constraint violation: 1048 Column ‘created_by’ cannot be null”

generated sql:

INSERT INTO article (name, netPchPrc, vat, groSalPrc, shop_id, created_by, updated_by, created_at, updated_at)

VALUES (‘some value…’, ‘4.2900’, ‘0.23’, ‘6.989967’, 2,

NULL, NULL, ‘1523222948’, ‘1523222948’)’

I have no clue why it’s behaving like that… Ofcourse there is no logged user because that’s an console part of the whole app, but if I understood corectly that’s exactly the case what the $dafaultValue property is designed for.

Any help would be appreciated.

Yii’s version: 2.0.15

Greetings, BartQ.

(sorry for my weak english, I haven’t used it for years…)

Hi.

I have found workaround for the problem.

With help of few experiments with debugger I have found that ‘blameableBehaviour’ method ‘getValue’ is looking for the ‘defaultValue’ property only if there is ‘user’ component in the application:




class BlameableBehavior extends AttributeBehavior

//[..]

protected function getValue($event)

    {

        if ($this->value === null && Yii::$app->has('user')) {

//[..]



I have added the ‘user’ component to console app configuration what has solved my problem.

However I think that having ‘user’ component as a must in console application is a little odd, especially if there is ‘defaultValue’ property which is designed on purpose of identity substitiution of logged user.

Please guys explain me if I just can’t see correct way of using it.

Greetings, BartQ.

if you want to put a default value for created_by you only need to overwrite

public function behaviors()
    {
        return [

            [
                'class' => BlameableBehavior::className(),
                'createdByAttribute' => 'created_by',
                'updatedByAttribute' => 'updated_by',
                'defaultValue' => '1',
            ],
}