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…)