Yii2: has_many gridview 和 detailview [英] Yii2: has_many gridview and detailview

查看:31
本文介绍了Yii2: has_many gridview 和 detailview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将在我的 Yii2 项目中使用第一种规范化形式,所以我添加了这样的表格
|身份证 |post_id |tag_id |
当我要发布模型时,我是这样写的:

公共函数getTags(){返回 $this->hasMany(PostTags::className(), ['post_id' => 'id']);}

在视图小部件中,我添加了tags.tag_id",但没有显示任何数据.
有没有办法在 DetailViewGridView 小部件中显示此标签?

也许,我可以在某处写group_concat"?

解决方案

我建议编写一个小部件来显示相关记录的链接列表.它是可重用的,防止在模型/控制器中生成 HTML,减少视图中的代码量.

models) {返回空;}$items = [];foreach ($this->models as $model) {$items[] = Html::a($model->{$this->linkContentBase}, [$this->viewRoute, 'id' => $model->id]);}返回 Html::ul($items, ['类' =>'list-unstyled','编码' =>错误的,]);}}

以下是一些示例(假设标签名称存储在 name 列中).

GridView中的使用:

<预><代码>['属性' =>'标签','格式' =>'生的','价值' =>功能($模型){/* @var $model common\models\Post */返回相关列表::小部件(['模型' =>$model->tags,'viewRoute' =>'/标签/视图',]);},],

DetailView中的使用:

/* @var $model common\models\Post */...['属性' =>'标签','格式' =>'生的','价值' =>相关列表::小部件(['模型' =>$model->tags,'viewRoute' =>'/标签/视图',]),],

不要忘记设置格式raw,因为默认情况下内容呈现为纯文本以防止XSS攻击(html特殊字符被转义).

您可以修改它以满足您的需要,这只是一个示例.

I'm going to use 1st normalization form in my Yii2 project, so I've added table like this
| id | post_id | tag_id |
and when I'm to posts model I've writed this:

public function getTags()
{
    return $this->hasMany(PostTags::className(), ['post_id' => 'id']);
}

In view widget I've added 'tags.tag_id', but it shows no data.
Is there any way to show this tags in DetailView and GridView widgets?

May be, I can write "group_concat" somewhere?

解决方案

I'd recommend to write a widget for displaying a links list of related records. It's reusable, prevents HTML generation in model / controller, reduces the amount of code in view.

<?php

namespace common\widgets;

use yii\base\Widget;
use yii\helpers\Html;

/**
 * Widget for display list of links to related models
 */
class RelatedList extends Widget
{
    /**
     * @var \yii\db\ActiveRecord[] Related models
     */
    public $models = [];

    /**
     * @var string Base to build text content of the link.
     * You should specify attribute name. In case of dynamic generation ('getFullName()') you should specify just 'fullName'.
     */
    public $linkContentBase = 'name';

    /**
     * @var string Route to build url to related model
     */
    public $viewRoute;

    /**
     * @inheritdoc
     */
    public function run()
    {
        if (!$this->models) {
            return null;
        }

        $items = [];
        foreach ($this->models as $model) {
            $items[] = Html::a($model->{$this->linkContentBase}, [$this->viewRoute, 'id' => $model->id]);
        }

        return Html::ul($items, [
            'class' => 'list-unstyled',
            'encode' => false,
        ]);
    }
}

Here are some examples (assuming tag name is stored in name column).

Usage in GridView:

[
    'attribute' => 'tags',
    'format' => 'raw',
    'value' => function ($model) {
        /* @var $model common\models\Post */
        return RelatedList::widget([
            'models' => $model->tags,
            'viewRoute' => '/tags/view',
        ]);
    },
],

Usage in DetailView:

/* @var $model common\models\Post */

...

[
    'attribute' => 'tags',
    'format' => 'raw',
    'value' => RelatedList::widget([
        'models' => $model->tags,
        'viewRoute' => '/tags/view',
    ]),        
],

Don't forget to set format raw, because by default content is rendered as plain text to prevent XSS attacks (html special characters are escaped).

You can modify this to fit your needs, this is just an example.

这篇关于Yii2: has_many gridview 和 detailview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆