2017-04-26 14:57:54 2603次浏览 4条回答 0 悬赏 10 金钱
    <?= DetailView::widget([
        'model' => $model,
        'attributes' => [
            [
                'attribute'=> 'user.nickname',
                'label'=>'Creater',
                'value'=>function($model){
                    return $model->'user.nickname'." ".$model->createdAt;
                }
            ],

$model->'user.nickname',这个正确写法应该是怎样的?现在报错!谢谢

最佳答案

  • 回答于 2017-04-26 15:34 举报

    什么啊,这是,闻所未闻。

  • 回答于 2017-04-26 15:39 举报

    源码上看起来是不支持匿名函数的,会报一个'The attribute configuration must be an array.'错误

    protected function normalizeAttributes()
        {
            if ($this->attributes === null) {
                if ($this->model instanceof Model) {
                    $this->attributes = $this->model->attributes();
                } elseif (is_object($this->model)) {
                    $this->attributes = $this->model instanceof Arrayable ? array_keys($this->model->toArray()) : array_keys(get_object_vars($this->model));
                } elseif (is_array($this->model)) {
                    $this->attributes = array_keys($this->model);
                } else {
                    throw new InvalidConfigException('The "model" property must be either an array or an object.');
                }
                sort($this->attributes);
            }
    
            foreach ($this->attributes as $i => $attribute) {
                if (is_string($attribute)) {
                    if (!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/', $attribute, $matches)) {
                        throw new InvalidConfigException('The attribute must be specified in the format of "attribute", "attribute:format" or "attribute:format:label"');
                    }
                    $attribute = [
                        'attribute' => $matches[1],
                        'format' => isset($matches[3]) ? $matches[3] : 'text',
                        'label' => isset($matches[5]) ? $matches[5] : null,
                    ];
                }
    
                if (!is_array($attribute)) {
                    throw new InvalidConfigException('The attribute configuration must be an array.');
                }
    
                if (isset($attribute['visible']) && !$attribute['visible']) {
                    unset($this->attributes[$i]);
                    continue;
                }
    
                if (!isset($attribute['format'])) {
                    $attribute['format'] = 'text';
                }
                if (isset($attribute['attribute'])) {
                    $attributeName = $attribute['attribute'];
                    if (!isset($attribute['label'])) {
                        $attribute['label'] = $this->model instanceof Model ? $this->model->getAttributeLabel($attributeName) : Inflector::camel2words($attributeName, true);
                    }
                    if (!array_key_exists('value', $attribute)) {
                        $attribute['value'] = ArrayHelper::getValue($this->model, $attributeName);
                    }
                } elseif (!isset($attribute['label']) || !array_key_exists('value', $attribute)) {
                    throw new InvalidConfigException('The attribute configuration requires the "attribute" element to determine the value and display label.');
                }
    
                $this->attributes[$i] = $attribute;
            }
        }
    
  • 回答于 2017-04-26 18:29 举报

    在你的model 类里面定义一个public 变量

    [
         'attribute'=> '你定义的变量',
         'label'=>'Creater',
         'value'=>function($model){
                return $model->user->nickname." ".$model->createdAt;
         }
    ]
    
您需要登录后才可以回答。登录 | 立即注册
阿江
副董事长

阿江

注册时间:2015-10-18
最后登录:2024-03-03
在线时长:186小时21分
  • 粉丝94
  • 金钱16816
  • 威望160
  • 积分20276

热门问题