Skip to content

Commit 73e1c3b

Browse files
thyseusthiagotalma
authored andcommitted
allow to search for user id in admin/index view (dektrium#905)
* allow to search for user id in admin/index view * apply table name to UserSearch#search
1 parent e64a251 commit 73e1c3b

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

models/UserSearch.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
*/
2222
class UserSearch extends Model
2323
{
24+
/** @var integer */
25+
public $id;
26+
2427
/** @var string */
2528
public $username;
2629

@@ -53,7 +56,7 @@ public function __construct(Finder $finder, $config = [])
5356
public function rules()
5457
{
5558
return [
56-
'fieldsSafe' => [['username', 'email', 'registration_ip', 'created_at', 'last_login_at'], 'safe'],
59+
'fieldsSafe' => [['id', 'username', 'email', 'registration_ip', 'created_at', 'last_login_at'], 'safe'],
5760
'createdDefault' => ['created_at', 'default', 'value' => null],
5861
'lastloginDefault' => ['last_login_at', 'default', 'value' => null],
5962
];
@@ -63,6 +66,7 @@ public function rules()
6366
public function attributeLabels()
6467
{
6568
return [
69+
'id' => Yii::t('user', '#'),
6670
'username' => Yii::t('user', 'Username'),
6771
'email' => Yii::t('user', 'Email'),
6872
'created_at' => Yii::t('user', 'Registration time'),
@@ -89,14 +93,17 @@ public function search($params)
8993
return $dataProvider;
9094
}
9195

96+
$table_name = $query->modelClass::tableName();
97+
9298
if ($this->created_at !== null) {
9399
$date = strtotime($this->created_at);
94-
$query->andFilterWhere(['between', 'created_at', $date, $date + 3600 * 24]);
100+
$query->andFilterWhere(['between', $table_name . '.created_at', $date, $date + 3600 * 24]);
95101
}
96102

97-
$query->andFilterWhere(['like', 'username', $this->username])
98-
->andFilterWhere(['like', 'email', $this->email])
99-
->andFilterWhere(['registration_ip' => $this->registration_ip]);
103+
$query->andFilterWhere(['like', $table_name . '.username', $this->username])
104+
->andFilterWhere(['like', $table_name . '.email', $this->email])
105+
->andFilterWhere([$table_name . '.id' => $this->id])
106+
->andFilterWhere([$table_name . 'registration_ip' => $this->registration_ip]);
100107

101108
return $dataProvider;
102109
}

views/admin/index.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@
3737
'filterModel' => $searchModel,
3838
'layout' => "{items}\n{pager}",
3939
'columns' => [
40+
[
41+
'attribute' => 'id',
42+
'headerOptions' => ['style' => 'width:90px;'], # 90px is sufficient for 5-digit user ids
43+
],
4044
'username',
4145
'email:email',
4246
[

0 commit comments

Comments
 (0)