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,
]);
}
Related
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]);
.
.
.
I have a basic gridview with Pajax established. It makes the ajax request but often carries out a reload. It always happens the second time it is filtered.
Controller
public function actionIndex()
{
$searchModel = new PersonSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
View
<?php
use yii\widgets\Pjax;
use yii\grid\GridView;
use kartik\select2\Select2;
?>
<div class="auth-user-index">
<?php Pjax::begin(['id'=>'pjax-users-gridview']); ?>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'identification_number',
],
]); ?>
<?php Pjax::end(); ?>
</div>
When you filter for the first time the parameters are corrected, the request is XHR. But the second time the request is Document and reload the page
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()
The following code is used to display gridview but it shows "data provider" error.
MODELS:
Subcategory.php:
public function getCountries()
{
return $this->hasOne(Subcategories::className(),['id'=>'catid']);
}
VIEW:
subcategory.php:
i have added this following coding to view the saved data in a grid view.
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns'=>[
['class'=>'yii\grid\SerialColumn'],
'id',
'categoryname',
[
'attribute' => subcategory,
'value'=>getCountries.subcatname,
],
['class'=>'yii\grid\ActionColumn'],
],
]); ?>
CONTROLLER
public function actionView2()
{
$dataProvider = new ActiveDataProvider([
'query' => Subcategory::find(),
'pagination' => [
'pageSize' => 20
]
]);
$posts = $dataProvider->getModels();
return $this->render('subcategory' [
'dataProvider' => $dataProvider,
]);
}
I have applied this coding but I get the following error:
PHP Notice – yii\base\ErrorException Undefined variable: dataProvider
I want to use ArrayDataProvider with the following code of siteController. I wrote the following code but it doesn't work.
Here is my actionIndex :
public function actionIndex()
{
$query = new \yii\db\Query;
$query->select('*')->from('business_main_categories');
$query->createCommand();
$dataProviders = [];
foreach ($query as $category) {
$dataProviders[] = new ArrayDataProvider([
'allModels' => $category,
'sort' => false,
'pagination' => false,
]);
}
return $this->render('index', [
'dataProvider' => $dataProviders,
]);
}
And want it to iterate in gridView. So, I wrote the following code (I don't know whether it's correct or not) :
Here is my index.php :
<?php
$dataProviders[] = 'dataProvider';
foreach ($dataProviders as $dataProvider) {
echo GridView::widget([
'dataProvider' => $dataProvider,
'summary' => '',
'columns' => [
[
'attribute' => 'bmc_image',
'format' => 'html',
'label' => '',
'value' => function ($data) {
return Html::img($data['bmc_image'],
['width' => '210px', 'height' => '190px']);
},
],
]
]);
}
?>
Controller
public function actionIndex()
{
$query = new \yii\db\Query;
$dataProvider = new ArrayDataProvider([
'allModels' =>$query->from('business_main_categories')->all(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
Index
<?= GridView::widget([
'dataProvider' => $dataProvider,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'attribute' => 'bmc_image',
'format' => 'html',
'value' => function ($data) {
return Html::img($data['bmc_image'],['width' => '210px', 'height' => '190px']);
},
],
],
]); ?>
I solved my problem without using gridview. As follows -
My SiteController -
public function actionIndex()
{
$searchModel = new BusinessMainCategoriesSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
$dataProvider->pagination->pageSize = $dataProvider->getTotalCount(); //-1 : disable
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
using this code I get all records in dataProvider from my db.
(Note that, I'm using ActiveDataProvider in my 'BusinessMainCategoriesSearch' model)
And, my index.php is -
<?php
$m = $dataProvider->getModels();
foreach ($m as $dp) {
echo "<img src = '".$dp['bmc_image']."' />";
echo '<center><font color = "white">'.$dp['bmc_name'].'<font/></center>';
}
?>
It worked great for me & it's a easiest way to do so.