I have a relation between User and Thesis and my dataProvider just displays user attributes and not thesis'ones in the Gridview. Is there a better way to print join attributes than: ['attribute'] => ['table.attribute']?
model:
public function search($params)
{
$query = Request::find()->joinWith('user')->joinWith('thesis')->where(['thesis.staff'=> Yii::$app->user->identity->id]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$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;
}
view:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
//'id',
'title',
['attribute' => 'thesis',
'value' => 'thesis.title'],
//'company',
'description:ntext',
'duration',
['attribute' => 'user'
'value' => 'user.id'],
//'requirements',
//'course',
'n_person',
'ref_person',
'is_visible:boolean',
//'created_at',
//'staff',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
controller:
public function actionIndex()
{
$searchModel = new SearchRequest();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
You need to add the relation getters on your model.
On your Thesis model, you need to add something like
public function getUser0 () {
return $this->hasOne(\your\user\model\location\User::className(), ['id' => 'user']);
}
Having this on your model will populate via lazy-load the user relation when you call Thesis::user0, in your case, something like this:
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
/// ---- other attributes
['attribute' => 'user0.name'],
/// ---- other attributes
],
]); ?>
Is better to add your code after $this->load($params);
My example is:
.
.
.
$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;
}
$query->joinWith('user');
$query->joinWith('thesis');
$query->andFilterWhere(['thesis.staff'=> Yii::$app->user->identity->id]);
.
.
.
Related
I want to use Select2 dr as filter in Yii Grid view, but it doesn't filter at all, only refreshes the page.
I take the data from 2 tables - from accounts I take only user_id, and from credits I take everything else. And every filter works, except And the 'user_id' one.
<?php
echo GridView::widget(
[
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'wp_id',
'value' => 'accounts.user_id',
'filter' => Select2::widget(
[
'model' => $searchModel,
'attribute' => 'wp_id',
'data' => ArrayHelper::map(Accounts::find()->all(), 'wp_id', 'user_id'),
'options' => ['placeholder' => ' --Filter by user id-- '],
'language' => 'en',
'pluginOptions' => [
'allowClear' => true,
],
]
),
],
],
]
); ?>
Here is the action in the controller.
<?php
public function actionIndex()
{
$searchModel = new CreditsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
} ?>
And this is the search model method
<?php
public function search($params)
{
$query = Credits::find()->with('accounts');
$dataProvider = new ActiveDataProvider([
'query' => $query
]);
$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;
}
if(!empty($this->user_id)) {
$query->andFilterWhere(['=', 'accounts.user_id', $this->user_id]);
}
// grid filtering conditions
$query->andFilterWhere([
'user_id' => $this->user_id,
'wp_id' => $this->wp_id,
'credits_bought' => $this->credits_bought,
'purchase_date' => $this->purchase_date,
'id' => $this->id,
]);
return $dataProvider;
}
} ?>
Thanks in advance!
Because you are passing the wp_id under the GridView filter and selecting the wp_id from the Accounts model as the value of the select2-dropdown
ArrayHelper::map(Accounts::find()->all(), 'wp_id', 'user_id')
Although it is confusing that you use the gridview column attribute wp_id and use the select2 to filter the user_id for the same column but if you are looking to do that you might need to change the above to
ArrayHelper::map(Accounts::find()->all(), 'user_id', 'wp_id')
Good day, great programmers, please I am using yii gridview to display students records but I want to restrict the records displayed to just a particular student. please how do I go about this ...this is my index
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
// 'id',
'student.first_name',
'student.last_name',
'studentFaculty.faculty_name',
'studentDept.department_name',
'level',
'stateOfOrigin.state_name',
'image',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
This is my controller below
public function actionIndex()
{
$searchModel = new StudentsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams)->ArrayHelper::map([ Students::find()->where('student_id'=> yii::$app->user->identity->id)->all()]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
The cleanest way to do this without adding any extra params to the search function because your search() function could be used for the backend by admin too and if you add extra params here you would need to modify them at all places it is called, so change your controller/action to the following and assign the student_id to the queryParams before calling the StudentsSearch model
public function actionIndex()
{
$searchModel = new StudentsSearch();
$queryParams=Yii::$app->request->queryParams;
$queryParams['StudentsSearch']['student_id']=Yii::$app->user->id;
$dataProvider = $searchModel->search($queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Just make sure your StudentsSearch model the field student_id is searched in the search() function before returning $dataProvider.
$query->andFilterWhere ( ['student_id' => $this->student_id ]);
Replace below code with Controller's Index action
public function actionIndex(){
$searchModel = new StudentsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->andWhere(['student_id'=>yii::$app->user->identity->id]);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Seems you are looking for something like this:
StudentsSearch model
public function search($params, $filterStudentId)
{
$query = Students::find();
$query->andWhere(['student_id' => $filterStudentId]);
// some code
return $dataProvider;
}
Controller
public function actionIndex()
{
$searchModel = new StudentsSearch();
$params = Yii::$app->request->queryParams;
$filterStudentId = Yii::$app->user->identity->id;
$dataProvider = $searchModel->search($params, $filterStudentId);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
i want to join my company table name to employee id. here my code in
employee model
public function attributeLabels()
{
return [
'id' => 'ID',
//'company_id' => 'Company ID',
'emp_name' => 'Emp Name',
'emp_email' => 'Emp Email',
'emp_salery' => 'Emp Salery',
];
}
public function getCompany()
{
return $this->hasOne(Company::className(),['id' => 'company_id']);
}
in index file i use this code
<?php PJax::begin();?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
//['class' => 'yii\grid\SerialColumn'],
//'id',
[
'attribute' => 'company_id',
'value' => 'company.name',
],
'emp_name',
'emp_email:email',
'emp_salery',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
<?php PJax::end();?></div>
in employee search table i use this code
public function search($params)
{
$query = Employee::find();
$query->joinWith(['company']);
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$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;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
]);
$query->andFilterWhere(['like', 'company_id', $this->company_id])
->andFilterWhere(['like', 'emp_name', $this->emp_name])
->andFilterWhere(['like', 'emp_email', $this->emp_email])
->andFilterWhere(['like', 'emp_salery', $this->emp_salery])
->andFilterWhere(['like', 'Company.name', $this->company_id])
->andFilterWhere(['like', 'company.name', $this->company])
;
return $dataProvider;
}
http://localhost/test/web/employee/
is shows in company field (not set)
pls help me
you can do this in two difference way : 1 . If you want use active record you must set property in first model you want to select it and join table with that like:
public $pin;
then when you get data you can access to pin in second table
the second way is you use asArray() that return all off data in both table
$model = News::find()->leftJoin('comment', 'news.id = comment.news_id')->asArray()->all();
then you can use it in your gridview
You should add a getter eg:getCompanyname()n your base model
......
public function attributeLabels()
{
return [
'id' => 'ID',
//'company_id' => 'Company ID',
'emp_name' => 'Emp Name',
'emp_email' => 'Emp Email',
'emp_salery' => 'Emp Salery',
'companyName' => 'Company Name')
];
}
public function getCompany()
{
return $this->hasOne(Company::className(),['id' => 'company_id']);
}
You should add a getter for company name
/*
Getter for company name
*/
public function getCommpanyName() {
return $this->company->name;
}
and in your gridview you should use the new get name (companyName)
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
'companyName',
'emp_name',
'emp_email:email',
'emp_salery',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
see this for more complete samples
http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/
I Have two tables "Personal_data" and "Students", Student has Personal_data.
I used Gii to generate the code for views, controlers and models, and I need to show all "Personal_data" from "Students" in a GridView, but i cant get it.
Code from models/PersonalDataSearch.php
public function search($params)
{
$query = PersonalData::find()->joinWith('Students'])->all();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'document' => $this->document,
'telephone' => $this->telephone,
'direction' => $this->direction,
'sex' => $this->sex,
]);
$query->andFilterWhere(['like', 'names', $this->names])
->andFilterWhere(['like', 'lastNames', $this->lastNames])
->andFilterWhere(['like', 'email', $this->email])
->andFilterWhere(['like', 'movile', $this->movile]);
return $dataProvider;
}
Code from controllers/PersonalDataController.php
public function actionIndex()
{
$searchModel = new PersonalDataSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
Code from views/personal-data/index.php
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'names',
'lastNames',
'email:email',
'movile',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
This view originally show all rows from Personal_data table from all registered people, but I want just show Personal_Data from Students, I added this line:
$query = PersonalData::find()->joinWith('Students'])->all();
and I have this error:
PHP Fatal Error – yii\base\ErrorException
Call to a member function andFilterWhere() on array
how can i solve this problem?
Remove ->all() from
$query = PersonalData::find()->joinWith('Students'])->all();
Data provider is taking care of fetching the data from DB, you just need to build the query (all() is fetching the data). With just a query you can add filters to it so there will be no error for andFilterWhere()
I have two tables staff with columns id, name and attendance. staff_id is used as foreign key in attendance table.
I want to display staff name in attendance gridview.
Attendance model:
public function getStaff()
{
return $this->hasOne(Staff::className(), ['id' => 'staff_id']);
}
public function getStaffName() {
return $this->staff->name;
}
and in index.php I used this code
<?= GridView::widget([
[
'attribute'=>'staff_id',
'value'=>'StaffName',
],
]); ?>
to get value of staff name. In this way I am getting staff name successfully but the problem is that when I make search for staff name in gridview it say "staff_id" should be integer as I define it as integer, but here I want to search name of staff instead of id.
How is this possible ? Thanks in advance
Add this in search model
$query->joinWith(['staff(relation name)']);
And add below code at filter query.
$query->andFilterWhere(['like', 'staff.name', $this->staff_id])
In staff.name that in staff is table name.
You can use this code
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
'staff.name',
],
]); ?>
OR use this code
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
[
'attribute' => 'staff.name',
'header' => 'Staff title'
'value'=> 'function ($model, $key, $index, $grid){
return $model->staff->name;
}'
],
],
]); ?>
OR in your code you can use this
<?= GridView::widget([
[
'attribute'=>'staff_id',
'value'=>'getStaffName()',
],
]); ?>
and for search you can watch this video Searching Related Table Data
You can use this code
1. relation in models
public function getCountry()
{
return $this->hasOne(Country::className(), ['id' => 'country_id']);
}
2. in grid view
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'state_name',
// 'country_id',
[
'attribute'=>'country_id',
'value'=>'country.country_name',
],
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
3. Search Models
change country_id from integer to safe than change on search function
public function search($params)
{
$query = State::find();
$query->joinWith(['country']);
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
]);
$query->andFilterWhere(['like', 'state_name', $this->state_name])
->andFilterWhere(['like', 'country.country_name', $this->country_id]);
return $dataProvider;
}