本文整理汇总了PHP中yii\base\Model::attributeFormats方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::attributeFormats方法的具体用法?PHP Model::attributeFormats怎么用?PHP Model::attributeFormats使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类yii\base\Model
的用法示例。
在下文中一共展示了Model::attributeFormats方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getDetailAttributes
/**
* Retrieves detail view attributes configuration using the modelClass.
* @param Model $model
* @param array $fields
* @return array detail view attributes
*/
public function getDetailAttributes($model, $fields)
{
if (!$model instanceof ActiveRecord) {
return $model->attributes();
}
/** @var ActiveRecord $model */
$formats = $model->attributeFormats();
$keys = self::getModelKeys($model);
list($behaviorAttributes, $blameableAttributes) = self::getModelBehaviorAttributes($model);
$attributes = $model->attributes();
$versionAttribute = $model->optimisticLock();
$result = [];
foreach ($fields as $key => $field) {
if (is_array($field)) {
$result[$key] = $field;
continue;
} elseif (!is_string($field) && is_callable($field)) {
$result[$key] = call_user_func($field, $model);
continue;
}
if (in_array($field, $attributes)) {
if (in_array($field, $keys) || in_array($field, $blameableAttributes) || $field === $versionAttribute) {
continue;
}
$result[$field] = ['attribute' => $field, 'format' => $formats[$field]];
continue;
}
$relation = $model->getRelation($field);
if (!$relation->multiple) {
// validate foreign keys only for hasOne relations
foreach ($relation->link as $left => $right) {
if (in_array($left, $blameableAttributes)) {
continue;
}
}
}
if (!Yii::$app->user->can($relation->modelClass . '.read')) {
continue;
}
$result[$field] = ['attribute' => $field, 'format' => 'crudLink', 'visible' => true, 'label' => $model instanceof ActiveRecord ? $model->getRelationLabel($relation, $field) : null];
}
return $result;
}