【问题标题】:Yii2 CRUD generated manuallyYii2 CRUD 手动生成
【发布时间】:2016-12-21 11:54:50
【问题描述】:

我想要一个 CRUD 表,具体来说我不需要编辑/删除记录,只有出现在 CRUD 生成表顶部的过滤结果部分是我的部分想要有。我的表包含来自数据库中 1 个表的数据,但我有一列未连接到该表或数据库中的任何其他表(它是一条注释,是基于表中的一列自动生成的)。我手动生成我的表格,但我想添加带有过滤功能的部分。我不知道该怎么做。在 Yii2 中是否可以手动完成,还是必须使用 CRUD 生成器?

【问题讨论】:

  • 你是说GridView吗?

标签: php yii2 crud


【解决方案1】:

我不使用 CRUD 生成器,因为它不会生成我想要的代码(而且我认为它也不会生成过滤器?)。我使用了一个基本模板,它几乎适合我需要显示的所有网格视图。这是一个可以为您提供过滤器的示例:

use yii\grid\GridView;

/** @var array $userTypes All user types (classes) */ 
// ...

echo GridView::widget([
    'dataProvider' => $modelProvider,
    'filterModel' => $model,
    'columns' => [
        [
            'attribute' => 'id',
            'format' => 'raw',
            'filter' => Html::input('text', 'User[id]', $model->id, ['class' => 'form-control', 'placeholder' => 'Filter ID']),
        [
            'attribute' => 'type',
            'format' => 'raw',
            'filter' => Html::activeDropDownList($model, 'type', $userTypes, ['class' => 'form-control', 'prompt' => 'All types']),
        ],
]);

在这里,我使用了 2 种不同的输入字段类型(文本和下拉菜单)。

对于Html::input,首先是类型(文本),然后是完整的属性名称(模型名称+属性名称),然后是默认值,最后是其他选项。

对于Html::activeDropDownList,我们首先有模型,然后是属性名称(仅),项目列表(数组),最后是其他选项。

【讨论】:

  • 如果我误解了你的问题,请澄清一下。
【解决方案2】:

我猜你说的是GridView,如果是,那么你可以在里面有自己的专栏,没问题。就像你提到的那样,让我们​​将该列称为comment

如果您使用由Gii 生成的基本模板,并且您还为该模型生成了搜索类,那么您comment 可以保护属性并为该代码添加代码以便能够过滤它。

如果您可以更详细地了解所提到的列、可能的值或算法,您可能会得到更合适的答案...

也可以看看Yii 2.0: Displaying, Sorting and Filtering Model Relations on a GridView

假设您的model 称为Xyz,因为您没有提供任何信息。我还将您表中的列命名为column_from_your_table,将您的虚拟列命名为comment

在您的模型Xyz 中,您将添加关系(具有特定名称的方法来定义它)

public function getComment()
{
    $column_from_your_table = $this->column_from_your_table;
    $comment = '';

    // your code to specify the relation
    // ...

    // this is value dislpayed in column grid
    return $comment;
}

在文件XyzSearch.php\app\models\

你会有这样的东西(当然可以根据你的需要进行编辑)

<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use yii\db\Expression;

/**
 * XyzSearch represents the model behind the search form about `app\models\Xyz`.
 */
class XyzSearch extends Xyz
{
    public $comment; // your virtual column

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            // add it to safe attributes
            [['comment'], 'safe'],
            // you will have more rules for your other columns from DB probably
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Xyz::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        // I dont know your how it is your comment column autogenerated
        // or what is the relation, so I give you just basic idea
        // your algorythm needs to be able to be rewritten in SQL
        // otherwise I dont know how you could possibly sort it
        $dataProvider->sort->attributes['comment'] = [
            'asc' => [Xyz::tableName().'.column_from_your_table' => SORT_ASC],
            'desc' => [Xyz::tableName().'.column_from_your_table' => SORT_DESC],
            'default' => SORT_ASC,
        ];

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // again, you will have more filtering conditions from your generated code

        // then you will add your custom filtering condition
        // I dont know your how it is your comment column autogenerated 
        // or what is the relation, so I give you just basic idea
        $query->andFilterWhere(['like', Xyz::tableName().'.column_from_your_table', $this->comment]);


        return $dataProvider;
    }
}

最后在您的 view 文件中添加您的虚拟列

<?php echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        // other columns from database
        // ...

        'comment',

        ['class' => 'yii\grid\ActionColumn'],
    ]
]); ?>

【讨论】:

    猜你喜欢
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    相关资源
    最近更新 更多