yii2从关系在gridview中添加列并在gridview中排序
我有两个问题 我有三张桌子。tbl_目标城市、lib_城市和lib_省份 通过视频教程,我能够将tbl_目标城市的数据显示到gridview,并将其连接到lib_城市 gridview中的排序工作得很好,但城市/市政列除外,它来自关系 问题1:如何更正城市/直辖市列的排序 问题2:如何添加带有搜索框和功能排序功能的省栏 待确定目标城市yii2从关系在gridview中添加列并在gridview中排序,yii2,yii2-advanced-app,Yii2,Yii2 Advanced App,我有两个问题 我有三张桌子。tbl_目标城市、lib_城市和lib_省份 通过视频教程,我能够将tbl_目标城市的数据显示到gridview,并将其连接到lib_城市 gridview中的排序工作得很好,但城市/市政列除外,它来自关系 问题1:如何更正城市/直辖市列的排序 问题2:如何添加带有搜索框和功能排序功能的省栏 待确定目标城市 +-------------------+-----------------------+ | Field | Type
+-------------------+-----------------------+
| Field | Type |
+-------------------+-----------------------+
| id | int(11) |
| city_code | varchar(20) |
| kc_classification | varchar(100) |
| cluster | enum('1','2','3','4') |
| grouping | int(11) |
| priority | varchar(100) |
| launch_year | int(11) |
+-------------------+-----------------------+
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| city_code | char(9) | NO | PRI | | |
| city_name | varchar(100) | NO | MUL | NULL | |
| prov_code | char(9) | NO | MUL | NULL | |
| is_Urban | tinyint(1) | YES | | 0 | |
| locked | tinyint(1) | YES | | 0 | |
| app_target_hh | int(5) | YES | | NULL | |
| 4p_areas | tinyint(1) | NO | | NULL | |
+---------------+--------------+------+-----+---------+-------+
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| prov_code | char(9) | NO | PRI | | |
| prov_name | varchar(60) | NO | MUL | NULL | |
| region_code | char(9) | NO | MUL | NULL | |
+-------------+-------------+------+-----+---------+-------+
自由城市
+-------------------+-----------------------+
| Field | Type |
+-------------------+-----------------------+
| id | int(11) |
| city_code | varchar(20) |
| kc_classification | varchar(100) |
| cluster | enum('1','2','3','4') |
| grouping | int(11) |
| priority | varchar(100) |
| launch_year | int(11) |
+-------------------+-----------------------+
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| city_code | char(9) | NO | PRI | | |
| city_name | varchar(100) | NO | MUL | NULL | |
| prov_code | char(9) | NO | MUL | NULL | |
| is_Urban | tinyint(1) | YES | | 0 | |
| locked | tinyint(1) | YES | | 0 | |
| app_target_hh | int(5) | YES | | NULL | |
| 4p_areas | tinyint(1) | NO | | NULL | |
+---------------+--------------+------+-----+---------+-------+
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| prov_code | char(9) | NO | PRI | | |
| prov_name | varchar(60) | NO | MUL | NULL | |
| region_code | char(9) | NO | MUL | NULL | |
+-------------+-------------+------+-----+---------+-------+
自由省
+-------------------+-----------------------+
| Field | Type |
+-------------------+-----------------------+
| id | int(11) |
| city_code | varchar(20) |
| kc_classification | varchar(100) |
| cluster | enum('1','2','3','4') |
| grouping | int(11) |
| priority | varchar(100) |
| launch_year | int(11) |
+-------------------+-----------------------+
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| city_code | char(9) | NO | PRI | | |
| city_name | varchar(100) | NO | MUL | NULL | |
| prov_code | char(9) | NO | MUL | NULL | |
| is_Urban | tinyint(1) | YES | | 0 | |
| locked | tinyint(1) | YES | | 0 | |
| app_target_hh | int(5) | YES | | NULL | |
| 4p_areas | tinyint(1) | NO | | NULL | |
+---------------+--------------+------+-----+---------+-------+
+-------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| prov_code | char(9) | NO | PRI | | |
| prov_name | varchar(60) | NO | MUL | NULL | |
| region_code | char(9) | NO | MUL | NULL | |
+-------------+-------------+------+-----+---------+-------+
TBL目标城市模型
<?php
namespace backend\models;
use Yii;
/**
* This is the model class for table "tbl_target_cities".
*
* @property integer $id
* @property string $city_code
* @property string $kc_classification
* @property string $cluster
* @property integer $grouping
* @property string $priority
* @property integer $launch_year
*/
class TblTargetCities extends \yii\db\ActiveRecord
{
public $province;
public $region;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'tbl_target_cities';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['region', 'kc_classification', 'cluster', 'grouping', 'priority', 'launch_year'], 'required'],
[['cluster','province','region'], 'string'],
[['grouping', 'launch_year'], 'integer'],
[['city_code'], 'string', 'max' => 20],
[['city_code'], 'required', 'message' => 'Please select a city or municipality.'],
[['kc_classification', 'priority'], 'string', 'max' => 100],
[['city_code'], 'unique','message'=>'City/Municipality is already covered.'],
[['province'], 'required','message'=>'You need to select province.'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'cityName.province.prov_name'=>'Province',//son2x:from the view.php file
'id'=>'City / Municipality',//son2x:from the view.php file
'city_code' => 'City / Municipality',
'kc_classification' => 'Kc Classification',
'cluster' => 'Cluster',
'grouping' => 'Grouping',
'priority' => 'Priority',
'launch_year' => 'Launch Year',
];
}
public static function get_cities($prov_code){
$select = "SELECT * FROM lib_cities where prov_code=$prov_code";
$query = Yii::$app->db->createCommand($select)->queryAll();
return $query;
}
//the first that works
/*public function getCityName()
{
return $this->hasOne(LibCities::className(),['city_code'=>'city_code']);
}*/
public function getCityName()
{
return $this->hasOne(LibCities::className(),['city_code'=>'city_code'])->with(['province']);
}
}
<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\TblTargetCities;
/**
* TblTargetCitiesSearch represents the model behind the search form about `backend\models\TblTargetCities`.
*/
class TblTargetCitiesSearch extends TblTargetCities
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[[ 'grouping', 'launch_year'], 'integer'],
[['id','city_code', 'kc_classification', 'cluster', 'priority'], 'safe'],
];
}
/**
* @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 = TblTargetCities::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->joinWith('cityName');
$query->andFilterWhere([
'id' => $this->id,
'grouping' => $this->grouping,
'launch_year' => $this->launch_year,
]);
/* $query->andFilterWhere(['like', 'city_code', $this->city_code])
->andFilterWhere(['like', 'kc_classification', $this->kc_classification])
->andFilterWhere(['like', 'cluster', $this->cluster])
->andFilterWhere(['like', 'priority', $this->priority]); */
$query->andFilterWhere(['like', 'kc_classification', $this->kc_classification])
->andFilterWhere(['like', 'cluster', $this->cluster])
->andFilterWhere(['like', 'priority', $this->priority])
->andFilterWhere(['like', 'lib_cities.city_name', $this->city_code]);
return $dataProvider;
}
}
<?php
namespace backend\models;
use Yii;
/**
* This is the model class for table "lib_cities".
*
* @property string $city_code
* @property string $city_name
* @property string $prov_code
* @property integer $is_Urban
* @property integer $locked
* @property integer $app_target_hh
* @property integer $4p_areas
*/
class LibCities extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'lib_cities';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['city_code', 'city_name', 'prov_code', '4p_areas'], 'required'],
[['is_Urban', 'locked', 'app_target_hh', '4p_areas'], 'integer'],
[['city_code', 'prov_code'], 'string', 'max' => 9],
[['city_name'], 'string', 'max' => 100]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'city_code' => 'City Code',
'city_name' => 'City Name',
'prov_code' => 'Prov Code',
'is_Urban' => 'Is Urban',
'locked' => 'Locked',
'app_target_hh' => 'App Target Hh',
'4p_areas' => '4p Areas',
];
}
public function getProvince()
{
return $this->hasOne(LibProvinces::className(),['prov_code'=>'prov_code']);
}
}
<?php
namespace backend\models;
use Yii;
/**
* This is the model class for table "lib_provinces".
*
* @property string $prov_code
* @property string $prov_name
* @property string $region_code
*/
class LibProvinces extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'lib_provinces';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['prov_code', 'prov_name', 'region_code'], 'required'],
[['prov_code', 'region_code'], 'string', 'max' => 9],
[['prov_name'], 'string', 'max' => 60]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'prov_code' => 'Prov Code',
'prov_name' => 'Prov Name',
'region_code' => 'Region Code',
];
}
}
按升序排列的示例视图-(工作不正常)
按降序
//it's for sort by city name
$dataProvider->sort->attributes['cityName'] = [
'asc' => ['lib_cities.cityName' => SORT_ASC],
'desc' => ['lib_cities.cityName' => SORT_DESC],
];
// it's sort by prov_name
$dataProvider->sort->attributes['province'] = [
'asc' => ['lib_provinces.prov_name' => SORT_ASC],
'desc' => ['lib_provinces.prov_name' => SORT_DESC],
];
[
'attribute'=>'city_code',
'value'=>'cityName.city_name',
//'contentOptions'=>['style'=>'width: 120px;']
],
[
'attribute'=>'province',
'value'=>'cityName.province.prov_name',
//'contentOptions'=>['style'=>'width: 120px;']
],
我通过将
search
方法更改为以下内容来计算排序
public function search($params)
{
$query = TblTargetCities::find();
$query->joinWith('cityName');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['city'] = [
// The tables are the ones our relation are configured to
// in my case they are prefixed with "tbl_"
'asc' => ['lib_Cities.city_name' => SORT_ASC],
'desc' => ['lib_Cities.city_name' => SORT_DESC],
];
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'grouping' => $this->grouping,
'launch_year' => $this->launch_year,
]);
/* $query->andFilterWhere(['like', 'city_code', $this->city_code])
->andFilterWhere(['like', 'kc_classification', $this->kc_classification])
->andFilterWhere(['like', 'cluster', $this->cluster])
->andFilterWhere(['like', 'priority', $this->priority]); */
$query->andFilterWhere(['like', 'kc_classification', $this->kc_classification])
->andFilterWhere(['like', 'cluster', $this->cluster])
->andFilterWhere(['like', 'priority', $this->priority])
->andFilterWhere(['like', 'lib_cities.city_name', $this->city_code]);
return $dataProvider;
}
参考资料:您只需添加$query->orderBy('lib\u cities.city\u name ASC');这是可行的,但如果我点击另一个标题,它总是按lib_cities排序…好的。我在试你的密码,我有个错误<代码>SQLSTATE[42S22]:未找到列:1054未知列“lib_province.prov_name”在“order子句”中正在执行的SQL是:选择
tbl_target_cities*FROM
tbl_target_cities`LEFT JOINlib_cities
ONtbl_target_cities
=lib_cities
城市
order BYlib_省
prov_name
LIMIT 20`从两个文件中注释省代码并检查。