012
(Zerododici)
February 19, 2015, 12:31pm
1
Hi all,
I’ve a form where the user could upload one or two file as optional field of the form.
<?= $form->field($model, 'file_front')->fileInput(); ?>
<?= $form->field($model, 'file_back')->fileInput(); ?>
I would store in db the userid that has upload file_front and the userid that has upload file_back.
In my model I would like to use in public function behaviors()
'blameable' => [
'class' => BlameableBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'created_by',
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_by'
],
],
is there a way to modify the db field ‘created_by’ when the use upload file_front e to modify ‘updated_by’ when the user upload file_back?
Thank you so much
timmy78
(Timothee Planchais)
February 19, 2015, 2:27pm
2
You can use the "value" callback.
Do something like this :
public function behaviors()
{
return [
[
'class' => BlameableBehavior::className(),
ActiveRecord::EVENT_BEFORE_INSERT => 'created_by',
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_by',
'value' => function ($event) {
if(isset($this->$owner->file_back)) {
return ...;
}
return ...;
},
],
];
}
012
(Zerododici)
February 20, 2015, 10:49am
3
Timmy78:
You can use the "value" callback.
Do something like this :
public function behaviors()
{
return [
[
'class' => BlameableBehavior::className(),
ActiveRecord::EVENT_BEFORE_INSERT => 'created_by',
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_by',
'value' => function ($event) {
if(isset($this->$owner->file_back)) {
return ...;
}
return ...;
},
],
];
}
Hi,
Do you mean something like that (I’ve changed $owner to owner as $owner gives the error: Undefined variable: owner)?
The fields submit_back_user and submit_front_user are never updated
Thank you
'blameable' => [
'class' => BlameableBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'created_by',
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_by',
],
'value' => function ($event) {
if(isset($this->owner->file_back)) {
return [
ActiveRecord::EVENT_BEFORE_INSERT => 'submit_back_user',
ActiveRecord::EVENT_BEFORE_UPDATE => 'submit_back_user',
];
}
if(isset($this->owner->file_front)) {
return [
ActiveRecord::EVENT_BEFORE_UPDATE => 'submit_front_user',
ActiveRecord::EVENT_BEFORE_INSERT => 'submit_front_user',
];
}
return [
ActiveRecord::EVENT_BEFORE_INSERT => 'created_by',
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated_by'
];
},
],
];
timmy78
(Timothee Planchais)
February 20, 2015, 12:00pm
4
Yes it is $this->owner sorry.
You have to return a value in the callable : http://www.yiiframework.com/doc-2.0/yii-behaviors-blameablebehavior.html
012
(Zerododici)
February 20, 2015, 12:20pm
5
Well, if I return a value this value will be store in ‘created_by’ or ‘updated by’ attribute that’s right?
What I want is that :
if a user create or update the others field of the form > his user_id is stored in ‘created_by’ or ‘updated by’ (and that is the “default” of BlameableBehavior class)
if a user insert or update the file_front field > his user_id is stored in the attribute ‘submit_front_user’
if a user insert or update the file_back field > his user_id is stored in the attribute ‘submit_back_user’
Really very thank you for your support