如何在yii2activerecord中获取属性标签

如何在yii2activerecord中获取属性标签,activerecord,yii2,Activerecord,Yii2,如何在Yii2中获取属性标签 我在Yii2文档中找到了这个函数getAttributeLabel(),我正在控制器中使用它。但它抛出了一个错误: 调用未定义的函数app\controllers\getAttributeLabel()试试这个 $model = new ModelName(); print_r($model->attributeLabels()); 如果您使用上述代码,您可以得到一个数组,其中包含模型的所有属性标签,因为Yii 2.0.13Active

如何在Yii2中获取属性标签

我在Yii2文档中找到了这个函数
getAttributeLabel()
,我正在控制器中使用它。但它抛出了一个错误:

调用未定义的函数app\controllers\getAttributeLabel()

试试这个

$model          =   new ModelName();
print_r($model->attributeLabels());

如果您使用上述代码,您可以得到一个数组,其中包含模型的所有属性标签,因为Yii 2.0.13
ActiveRecord
实现了这个数组,所以您可以使用它来获取模型的静态实例。使用它应该比手动创建模型实例以使用其非静态方法更干净、更高效

$task = new Task();

//to get single attribute label
$label = $task->getAttributeLabel('task_title');

//to get all attribute label
$labels = $task->attributeLabels();
$singleLabel = MyModel::instance()->getAttributeLabel('my_attribute');

$allLabels = MyModel::instance()->attributeLabels();

是否要查看模型的所有属性标签?