Answer the question
In order to leave comments, you need to log in
How to get all properties (attributes) of a model in an attached behavior?
Actually here is the behavior
use yii\base\Behavior;
class ExportBehavior extends Behavior
{
public $attributes;
public function export(){
return $this->attributes;
}
}
public function behaviors() {
return [
'export'=>[
'class'=>ExportBehavior::class,
'attributes'=>$this->attributes,
]
];
}
array (size=10)
'id' => null
'firstname' => null
'lastname' => null
'middlename' => null
'phone' => null
'state' => null
'date' => null
'comment' => null
'created_at' => null
'updated_at' => null
Answer the question
In order to leave comments, you need to log in
You are passing the attributes at the moment you attach the behavior to the model, and at that time they are not populated. If you want to get up-to-date data, refer to the model attributes in the behavior itself using the owner
property
, for example:
class ExportBehavior extends Behavior
{
public $attributes;
public function export(){
return $this->owner->attributes;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question