How to display one CGridView from two databases - php

i am trying to display two database tables source in one CGridView..
2 tables were reg.students and login.user..
my students model relation is,
public function relations()
{
Yii::app()->getModule('user');
return array(
'royaltyOutstandings' => array(self::HAS_MANY, 'RoyaltyOutstanding', 'studentID'),
'srkMedicalInfos' => array(self::HAS_MANY, 'SrkMedicalInfo', 'studentID'),
'parents' => array(self::HAS_ONE, 'SrkParents', 'studentID'),
'srkStudentWorksheets' => array(self::HAS_MANY, 'SrkStudentWorksheet', 'studentID'),
'user' => array(self::BELONGS_TO, 'User', 'centre_id'),
);
}
In user module,
public function tableName()
{ return 'login.user'; }
in cgridview columns array,
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'students-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'header' => 'No.',
'value' => '$row+1',
),
array('name' => 'user.centre_id',
'value'=>'$data->user->centre_id',
),
'... // & so on
Controller action is,
public function actionAdmin()
{
$this->layout = 'column3';
$form = new Reports ;
$model=new Students('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Students']))
$model->attributes=$_GET['Students'];
$this->render('admin',array(
'model'=>$model,
'form'=>$form,
));
}

If I understand you correctly, you want to show one or more relations in your gridview.
If this is what you want, have a look at this tutorial: http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/
This tutorial doesn't only show you how to show a relation in your gridview but it also shows you how to sort and filter the data. good luck!

Related

How to assign new values to saved data in yii 1?

I have following code in my controller:
public function actionAdmin()
{
$model=new MForm('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['ChManageForm']))
$model->attributes=$_GET['ChManageForm'];
$this->render('admin',array(
'model'=>$model,
));
}
and
const member=1;
const district=2;
My view(called admin)
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'ch-manage-form-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'form_name',
'region',
'phone_number',
'email',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
1 and 2 appears in the column called name(in my view file) and values of this column are saved in integer format. I need to show(in my view(admin)) member and district instead of numbers(e.g member instead of 1). How can I do it?
You can also specify the grid view column like this (doesn't require a helper in your model):
array(
'name'=>'form_name',
'value'=>'$data->form_name ? \'Member\':\'District\'',
'type'=>'text',
),
this works great if you only have 2 value in the variable.
Visit this URL to get more details.
http://www.yiiframework.com/forum/index.php/topic/14845-if-condition-inside-cgridview/
EDIT:
IF you have more than 2 values,
Model which you use to CGridView:
public function getValueText() {
return $this->getValueTextOptions[$this->form_name];
}
public function getValueTextOptions() {
return array(
1 => 'Member',
2 => 'District',
);
}
View with CGridView:
array(
'name'=>'form_name',
'value'=>'$data->getValueText()',
)

Yii: Filter with CActiveDataProvider in grid view gives error

I am trying to use filters in cGridview with cActiveDataProvider but it gives following exception
CActiveDataProvider and its behaviors do not have a method or closure named "getValidators"
Please check following code
Controller
public function actionAdmin()
{
$admin_type=Yii::app()->user->isAdmin;
if($admin_type==1)
{
$admin_batches=WebHelper::getAllAdminBatchesInArray(Yii::app()->user->getId());
if(!empty($admin_batches))
{
$batch_list=implode(",",$admin_batches);
}
$batch_criteria="group_id IN ( ".$batch_list." ) and status!=-1";
}
else
$batch_criteria="1 and status!=-1";
$model=new CActiveDataProvider('Users',array('criteria'=>array(
'condition'=>$batch_criteria,
)));
// print_r($model);die;
//$model->unsetAttributes(); // clear any default values
if(isset($_GET['Users']))
$model->attributes=$_GET['Users'];
$this->render('admin',array(
'model'=>$model,
));
}
View
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'users-grid',
'dataProvider'=>$model,
'filter'=>$model,
'columns'=>array(
array(
'class'=>'CButtonColumn',
'htmlOptions'=>array('width'=>'70px'),
'buttons' => array(
'view' => array(
'imageUrl' => Yii::app()->baseUrl . '/themes/gamify/images/view-image.png'
),
'update' => array(
'imageUrl' => Yii::app()->baseUrl . '/themes/gamify/images/Edit-icon.png'
),
'delete' => array(
'imageUrl' => Yii::app()->baseUrl . '/themes/gamify/images/Delete-icon.png'
)
)
),
first_name',
'last_name',
'user_name',
'group.group_title',
//'password',
'email_id',
array(
'type'=>'raw',
'name'=>'Status',
'value'=>'($data->status==0)?"<a href=\'\' id=\'$data->user_id\' class=\'user_status Active\'>Active</a>":"<a href=\'\' id=\'$data->user_id\' class=\'user_status Inactive\'>Inactive</a>"'
),
),));?>
I generated a new yii project and made a company table in my db, Then I generated a CRUD in gii for testing. This is the default structure for actionAdmin in every generated yii controller:
public function actionAdmin() {
$model = new Company('search');
$model->unsetAttributes(); // clear any default values
if (isset($_POST['Company']))
$model->attributes = $_POST['Company'];
$this->render('admin', array(
'model' => $model,
));
}
And in the default admin view for every generated CRUD, yii passes $model to the CGridView filter attribute. This means that, CGridView's filter accepts only Model's Object, but you pass instance of CActiveDataProvider to it. Suppose $model is a model object(For example, Users in your case), You should pass $model to filter of CGridView and pass $model->search() to dataProvider of CGridView. Then you can build your Criteria inside of $model->search() method. I recommend you to generate a sample CRUD in yii and see generated code.

Cgridview In Parentview

Yii 1.1.14
I have an employee table and a comment table
In the employee view i want to show all the comments of the employee in a grid (after the employee fields)
I tried to follow the example here
Here is where I am :
New code to defining a new search function in the model :
public $commentdate_param;
public $commentobservation_param;
...
public function rules()
{
return array(
...
array('public $commentdate_param, commentobservation_param, ...', 'safe', 'on'=>'search, searchIncludingComments'),
);
public function relations()
{
return array(
...
'employeecomments' => array(self::HAS_MANY, 'Employeecomments', 'employee_id'),
);
}
public function searchIncludingComments($parentID)
{
$criteria=new CDbCriteria;
$criteria->with=array('employeecomments');
$criteria->together = true;
$criteria->compare('t.employee_id',$parentID,false);
$criteria->compare('employeecomments.date', $this->commentdate_param,true);
$criteria->compare('employeecomments.observation', $this->commentobservation_param,true);
$sort = new CSort;
$sort->attributes = array(
'commentdate_param' => array(
'asc' => 'date_desc',
'desc' => 'date_desc DESC',
), '*', /* Treat all other columns normally */
);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>$sort,
));
}
Adding code in the controller actionView :
...
$child_model = new Employee("searchIncludingComments");
$child_model->unsetAttributes();
$this->render('view',array(
'model'=>$this->loadModel($id),
'child_model'=>$child_model,
'parentID' => $id
));
Adding code in the view :
...
<h3>Comments</h3>
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'child-grid',
'dataProvider'=>$child_model->searchIncludingComments($parentID),
'filter'=>$child_model,
'columns'=>array(
'date',
'observation',
array(
'class'=>'CButtonColumn',
),
),
));
...
But I must have missed something because when it comes to grid rendering stops - but no error in log
Would be nice if somebody could help me !
I found the solution for me - all my changes and controler are worthless because now I do all stuff in the view :
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'comments-grid',
'dataProvider'=>new CActiveDataProvider('Employeecomment',array(
'criteria'=>array(
'condition'=>'employee_id=:eid',
'params'=>array(':eid'=>$model->id),
),
'sort'=>array(
'defaultOrder'=>'date DESC',
),
)),
Sure there is something I did not understand why it did not work ...

Displaying attributes from another model using CGRIDVIEW

I have 2 tables/models:
Tmp1
Header
QuestionText
Tmp2
Header
Part
OutOf
I'm trying to display the columns: Header, QuestionText, Part, OutOf
in a single CGRIDVIEW.
In Tmp1 model:
public function relations()
{
return array(
'tmp2s' => array(self::HAS_MANY, 'Tmp2', 'Header'),
);
}
In Tmp2 Model:
public function relations()
{
return array(
'header' => array(self::BELONGS_TO, 'Tmp1', 'Header'),
);
}
Controller:
public function actionReviewAll()
{
$tmp1 = new Tmp1('search');
$tmp1->unsetAttributes();
if(isset($_GET['Tmp1'])){
$model->attributes=$_GET['Tmp1'];
}
$this->render('ReviewAll',array(
'tmp1'=>$tmp1,
));
}
View:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tmp-grid',
'dataProvider'=>$tmp1->search(),
'filter'=>$tmp1,
'columns'=>array(
'Header',
'QuestionText',
array(
'name' => 'tmp2.OutOf',
'value' => '$data->tmp2[0].OutOf',
'type' => 'raw'
),
),
)); ?>
Error:
Property "Tmp1.tmp2" is not defined.
Any help is greatly appreciated
You have a sintax error:
Tmp1.tmp2 = Tmp1.tmp2s
The relation alias is with 's'. "tmp2s".
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'tmp-grid',
'dataProvider'=>$tmp1->search(),
'filter'=>$tmp1,
'columns'=>array(
'Header',
'QuestionText',
'tmp2s.Part',
'tmp2s.OutOf',
),
)); ?>
But you are trying to show a 'self::HAS_MANY' relationship, you can't show that on a normal CGridView widget...
Your code probably works (I haven't tried it), but not in cases where your tmp1 model has no tmp2's. You must make sure there is a tmp2 before accessing it:
'value' => 'isset($data->tmp2) ? $data->tmp2[0].OutOf : Null',
You can also pass a function($data,$row) to value to make it look less messy. (Yes, that's important to me.)

yii "Trying to get property of non-object" at view.php

I have a strange problem with my yii application. I have two tables in my database Players - id, name, team_id and Teams - id, name. I can create new players but when I want to see player's profile there is an error -"Trying to get property of non-object" at this line:
'value'=>$model->team->NAME,
The most strange problem is that when I test url for players with Ids 1 and 2 everything is okay and I see the correct information, but for other ids I have this problem. Here is part of my code:
view.php
<?php $this->widget('zii.widgets.CDetailView', array(
'data'=>$model,
'attributes'=>array(
'ID',
'NAME',
'TEAM_ID',
array(
'label'=>'Отбор',
'type'=>'text',
'value'=>$model->team->NAME,
),
),
)); ?>
Players.php
public function relations()
return array(
'team' => array(self::BELONGS_TO, 'TEAMS', 'ID'),
);
}
Teams.php
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'player' => array(self::HAS_MANY, 'PLAYERS', 'ID'),
);
}
PlayersController.php
public function actionView($id)
{
$teams = new CActiveDataProvider('Teams');
$players = new CActiveDataProvider('Players');
$this->render('view', array(
'model'=>$this->loadModel($id),
));
}
Seems like you need to fix bug with relations in Players model:
public function relations()
return array(
'team' => array(self::BELONGS_TO, 'TEAMS', 'TEAM_ID'), // TEAM_ID instead of ID
);
}

Categories