Daunio
Daunio

Reputation: 61

PHP Yii2 - Get attributes name of an Object

I was wondering if there is a way to get just the attributes name? In the example I mean: id, name, user. I don´t need the values, just name of attributes.

app\models\Data Object
(
[_attributes:yii\db\BaseActiveRecord:private] => Array
    (
        [id] => 1
        [name] => 
        [user] => 
    )

[_oldAttributes:yii\db\BaseActiveRecord:private] => Array
    (
        [id] => 1
        [name] => 
        [user] =>             
    )    
)

Thanks!

Upvotes: 1

Views: 10473

Answers (1)

Jap Mul
Jap Mul

Reputation: 18769

You can do it like the code in the comment but Yii also provides a function on ActiveRecord itself to get the attribute names:

$model->attributes();

Official docs: http://www.yiiframework.com/doc-2.0/yii-db-activerecord.html#attributes()-detail

Upvotes: 8

Related Questions