Yii2 CRUD 手动生成
Yii2 CRUD generated manually
我想要一个 CRUD table,具体来说我不需要 edit/delete 记录,只需要有过滤结果的部分,它出现在 CRUD 的顶部generated table, 是我想要的部分。我的 table 包含数据库中 1 table 的数据,但我有一个列未连接到数据库中的此列或任何其他 table (这是一条评论,是自动生成的,基于 table 中的一列)。我手动生成我的 table,但我想添加带过滤的部分。我不知道该怎么做。在 Yii2 中是否可以手动完成,还是必须使用 CRUD 生成器?
我不使用 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
,我们首先有模型,然后是属性名称(仅),项目列表(数组),最后是其他选项。
我猜你说的是 GridView,如果是,那么你可以在里面有自己的专栏,没问题。让我们像你提到的那样调用该列 comment
如果您使用由 Gii 生成的基本模板,并且还为该模型生成搜索 class,那么您 comment
到安全属性并将该代码的代码添加到能够过滤它。
如果您能更详细地了解提到的列、可能的值或算法,您可能会得到更多 suitable 答案...
也看看Yii 2.0: Displaying, Sorting and Filtering Model Relations on a GridView
假设您的 model
被称为 Xyz
,因为您没有提供任何信息。此外,我将您的 table 中的列命名为 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'],
]
]); ?>
我想要一个 CRUD table,具体来说我不需要 edit/delete 记录,只需要有过滤结果的部分,它出现在 CRUD 的顶部generated table, 是我想要的部分。我的 table 包含数据库中 1 table 的数据,但我有一个列未连接到数据库中的此列或任何其他 table (这是一条评论,是自动生成的,基于 table 中的一列)。我手动生成我的 table,但我想添加带过滤的部分。我不知道该怎么做。在 Yii2 中是否可以手动完成,还是必须使用 CRUD 生成器?
我不使用 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
,我们首先有模型,然后是属性名称(仅),项目列表(数组),最后是其他选项。
我猜你说的是 GridView,如果是,那么你可以在里面有自己的专栏,没问题。让我们像你提到的那样调用该列 comment
如果您使用由 Gii 生成的基本模板,并且还为该模型生成搜索 class,那么您 comment
到安全属性并将该代码的代码添加到能够过滤它。
如果您能更详细地了解提到的列、可能的值或算法,您可能会得到更多 suitable 答案...
也看看Yii 2.0: Displaying, Sorting and Filtering Model Relations on a GridView
假设您的 model
被称为 Xyz
,因为您没有提供任何信息。此外,我将您的 table 中的列命名为 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'],
]
]); ?>