Php 手动生成的Yi2积垢

Php 手动生成的Yi2积垢,php,yii2,crud,Php,Yii2,Crud,我希望有一个CRUD表,具体来说,我不需要编辑/删除记录,只有显示在CRUD生成表顶部的具有过滤结果的部分才是我想要的部分。我的表包含数据库中1个表中的数据,但有一列未连接到此表或数据库中的任何其他表(它是一条注释,根据表中的一列自动生成)。我手动生成我的表,但我想添加带有筛选的部分。但是我不知道怎么做。在Yii2中是否可以手动执行,或者我必须使用CRUD生成器?我不使用CRUD生成器,因为它不会生成我想要的代码(我认为它也不会生成过滤器?)。我使用了一个基本的模板,它适用于几乎所有需要显示的G

我希望有一个CRUD表,具体来说,我不需要编辑/删除记录,只有显示在CRUD生成表顶部的具有过滤结果的部分才是我想要的部分。我的表包含数据库中1个表中的数据,但有一列未连接到此表或数据库中的任何其他表(它是一条注释,根据表中的一列自动生成)。我手动生成我的表,但我想添加带有筛选的部分。但是我不知道怎么做。在Yii2中是否可以手动执行,或者我必须使用CRUD生成器?

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

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']),
        ],
]);
在这里,我使用两种不同的输入字段类型(文本和下拉)

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


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

我想你是在说,如果是,那么你可以在其中有自己的列,没问题。让我们调用您提到的列
comment

如果您使用由生成的基本模板,并且还为该模型生成搜索类,那么您可以将
注释
添加到安全属性中,并为该代码添加代码,以便能够对其进行过滤

若你们们能更详细地了解所提到的列,可能的值或算法,你们们可能会得到更合适的答案

也看看

假设您的
模型
被称为
Xyz
,因为您没有提供任何。此外,我还将您表中的列命名为您表中的
column\u
,将您的虚拟列命名为
comment

在模型
Xyz
中,您将添加关系(使用特定名称定义关系的方法)

\app\models\

您将有类似的内容(当然,根据您的需要进行编辑)


你是说GridView吗?如果我误解了你的问题,请澄清。
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;
}
<?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;
    }
}
<?php echo GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],

        // other columns from database
        // ...

        'comment',

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