yii CGridView dispaly data from relative tbls - php

I have a small problem. I created user CGridView which is relationship with company and department.
Error : company name is display in the grid view. but department name is not display in gridview.
When I uncomment 'value'=>....., "Trying to get property of non-object" show.
This model relation
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(
'ranks' => array(self::BELONGS_TO, 'Rank', 'rank_id'),
'companies' => array(self::BELONGS_TO, 'Company', 'company_id'),
'departments' => array(self::BELONGS_TO, 'Department', 'department_id'),
'departmentsdep' => array(self::BELONGS_TO, 'Department', 'department_id'),
'departmentssec' => array(self::BELONGS_TO, 'Department', 'section_id'),
'departmentstea' => array(self::BELONGS_TO, 'Department', 'team_id'),
);
}
this is gridview
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'login_name',
'first_name',
'last_name',
'email',
array(
'name' => 'company_id',
'value'=>'$data->companies->name',
'filter'=>CHtml::listData($companylist, 'id', 'name'),
),
array(
'name' => 'department_id',
//'value'=>'$data->departmentsdep->name',
'filter' =>
CHtml::listData(
is_numeric($model->company_id) ? Department::model()->findAll(new CDbCriteria(array(
'condition' => 'p_id = 0 AND company_id=:company_id',
'params' => array(':company_id' => $model->company_id),
))) : $department, 'id', 'name'),
),
array(
'name' => 'section_id',
//'value'=>'$data->departmentssec->name',
'filter' =>
CHtml::listData(
is_numeric($model->department_id) ? Department::model()->findAll(new CDbCriteria(array(
'condition' => 'p_id = :pid AND company_id=:company_id',
'params' => array(':company_id' => $model->company_id, ':pid'=>$model->department_id),
))) : $department, 'id', 'name'),
),
array(
'name' => 'team_id',
//'value'=>'$data->departmentstea->name',
'filter' =>
CHtml::listData(
is_numeric($model->department_id) ? Department::model()->findAll(new CDbCriteria(array(
'condition' => 'p_id = :pid AND company_id=:company_id',
'params' => array(':company_id' => $model->company_id, ':pid'=>$model->section_id),
))) : $department, 'id', 'name'),
),
array(
'class'=>'CButtonColumn',
),
),
)); ?>
How can I solve this error

This is my solution
'value' => '($data->department_id == 0) ? "" : $data->departmentsdep->name',
full code
array(
'name' => 'department_id',
'value' => '($data->department_id == 0) ? "" : $data->departmentsdep->name',
'filter' =>
CHtml::listData(
is_numeric($model->company_id) ? Department::model()->findAll(new CDbCriteria(array(
'condition' => 'p_id = 0 AND company_id=:company_id',
'params' => array(':company_id' => $model->company_id),
))) : $department, 'id', 'name'),
),

You can access relations using a '.' to sign a relation. It would be much easier this way:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'login_name',
'first_name',
'last_name',
'email',
array(
'name' => 'companies.name',
'filter'=>CHtml::listData($companylist, 'id', 'name'),
),
...

Related

How to display multiple student name in zii.widgets.grid.CGridView from database in admin page?

<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'tasks-grid',
'dataProvider' => $model->search($tmp),
'itemsCssClass' => 'table table-striped table-hover table-bordered',
'filter' => $model,
'columns' => array(
'task_id',
array('header' => 'Sr. No.',
'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)', 'id' => 'colour', 'filter' => 'Search By :'),
array('name' => 'pm_id',
'value' => '$data->project1->project'),
'task_title',
'assigned_to',
'priority',
'status',
?>
I want to display assigned_to multiple student names like this format on gideView, for example:
Rahul,Nikesh,Omkar.
but it display like this eg Rahul (it displays only one name but in my database table 1,27,26 (student ids) on assigned_to columns).
'task_title',
'assigned_to',
'priority',
'status'
This is my four column in database table.
assuming your relation assigned_to is a HAS_MANY or MANY_MANY relation you could do something like:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'tasks-grid',
'dataProvider' => $model->search($tmp),
'itemsCssClass' => 'table table-striped table-hover table-bordered',
'filter' => $model,
'columns' => array(
'task_id',
array('header' => 'Sr. No.',
'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)', 'id' => 'colour', 'filter' => 'Search By :'),
array('name' => 'pm_id',
'value' => '$data->project1->project'),
'task_title',
array(
'name'=>'assigned_to',
'value'=>'implode(",", array_values(CHtml::listData($data->assigned_to)))'
),
'priority',
'status',
)
));

Yii view not work, EColumnsDialog

picture shows what I have and what they want to have
'columns' => array(
array(
'header' => 'id',
'name' => 'id',
'value'=>'$data->id',
),
I have to do so that the "value => id" instead display an NO.ID Started to display an TXT_ ID NO.ID
By using createCommand
Yii::app()->db->createCommand('SELECT CONCAT('TXT ',id),other_fields.. FROM table WHERE 1');
This will definitely help you
'columns' => array(
array(
'header' => 'id',
'name' => 'id',
'value' =>'$data->id',
'type' =>'raw',
"value" => function($data){
echo "TXT ".$data->id;
},
),

Yii filter results without relation

I am using CGridView in Yii which is listing results with some of relations from other models
<?php $this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$model->search(),
'filter' => $model,
'columns' => array(
array(
'name' => 'user.name',
'header' => Yii::t('app', 'User'),
'value' => '!isset($data->user->name) ? Yii::t("app", "Unknown user") : $data->user->name',
'type' => 'raw',
'filter' => CHtml::activeTextField($model, 'keyName'),
),
array(
'name' => 'visitor.company',
'header' => Yii::t('app', 'Company'),
'value' => 'isset($data->visit->visitor->company) ? $data->visit->visitor->company : (isset($data->rfid->rfid) ? Yii::t("app", "Company 1") : Yii::t("app", "Unknown company"))',
'type' => 'raw',
'filter' => CHtml::activeTextField($model, 'visitorCompany'),
),
array(
'name' => 'pass_id',
'header' => Yii::t('app', 'Pass'),
'value' => '!isset($data->pass_id) ? Yii::t("app", "Massing pass") : "<span class=\"label label-primary\">" . $data->pass_id . "</span>"',
'type' => 'raw',
),
),
)); ?>
and there is isset() condition which checks if relation exists (otherwise an error occurs)
But now I am looking for a way to filter those results which. For example to write "Unknown" in the filter to get the results without relations in the other table or "Company" to get those without some relation but with another existing relation

Yii: Can not Display Data in Grid View

I can't list data in grid using yii framework. My controller is Sitecontroller.php, My view is list_jobseeker.php.
I got the error:
Parse error: syntax error, unexpected '*', expecting ']' in C:\wamp\www\yii_new\framework\base\CComponent.php(612) : eval()'d code on line 1
Anybody give any suggestion to correct these issue?
My controller:
public function actionlist_jobseeker()
{
$session_id=Yii::app()->session['user_id'];
if ($session_id == "")
{
$this->redirect( array('/employee/site/login'));
}
$user_id =$session_id;
$items = Yii::app()->db->createCommand()
->select('*')
->from('job_seeker_profile s')
->join('job_profile j','s.user_id = j.user_id')
->order('s.id')
->queryAll();
$this->render('list_jobseeker',array('items' =>$items));
}
My view page - list_jobseeker.php
<h1>View Jobseeker</h1>
<div class="flash-success">
</div>
<div class="form">
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'rates-phase-grid',
'htmlOptions' => array('class' => 'table table-striped table-bordered table-hover'),
'dataProvider'=>new CArrayDataProvider($items),
'columns' => array(
array(
'name' => 'Name',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->name)',
'htmlOptions' => array('style'=>'width:90px;','class'=>'zzz'),
),
array(
'name' => 'Email',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->email)',
'htmlOptions' => array('style'=>'width:250px;','class'=>'zzz')
),
array(
'name' => 'Password',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->password)',
'htmlOptions' => array('style'=>'width:90px;','class'=>'zzz')
),
array(
'name' => 'Contact No',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->contact_no)',
'htmlOptions' => array('style'=>'width:40px;','class'=>'zzz')
),
array(
'name' => 'Gender',
'type' => 'raw',
'value' => 'CHtml::encode($data[*]->gender)',
'htmlOptions' => array('style'=>'width:40px;','class'=>'zzz')
),
array(
'class' =>'CButtonColumn',
'deleteConfirmation'=>'Are you sure you want to delte this item?',
'template'=>'{update}{delete}',
'buttons' =>array('update'=>array(
'label'=>'edit',
'url'=>'Yii::app()->controller->createUrl("UpdateJob",array("id"=>$data["id"]))',
),
'delete'=>array('label'=>'delete',
'url'=>'Yii::app()->controller->createUrl("DeleteJob",array("id"=>$data["id"]))'),
)
)
),
));
?>
</tbody>
change all the [*]
$data[*]->name
to match columns
$data["name"]

yii CGridView filter with relations

I'm using yii for my web application. In one of my view I have CGridView and dataprovider is Mail model. In this model I have relation with with 3 other models. In the grid I show cols from three models. How can I filter the CGridView?
UPDATE:
<?php $dialog = $this->widget('ext.ecolumns.EColumnsDialog', array(
'options'=>array(
'title' => 'Layout settings',
'autoOpen' => false,
'show' => 'fade',
'hide' => 'fade',
),
'htmlOptions' => array('style' => 'display: none'), //disable flush of dialog content
'ecolumns' => array(
'gridId' => 'mails-grid', //id of related grid
'storage' => 'session', //where to store settings: 'db', 'session', 'cookie'
'fixedLeft' => array('CCheckBoxColumn'), //fix checkbox to the left side
'model' => $dataprovider, //model is used to get attribute labels
'columns'=>array(
array(
'name'=>'mailTemplate.name',
'filter'=>CHtml::activeTextField($dataprovider, 'mailTemplate'),
),
'sendDate',
array(
'name'=>'mailTemplate.subject',
'filter'=>CHtml::activeTextField($dataprovider, 'mailTemplate'),
),
array(
'name'=>'client.email',
'filter'=>CHtml::activeTextField($dataprovider, 'client'),
),
array(
'name'=>'client.name',
'filter'=>CHtml::activeTextField($dataprovider, 'client'),
),
array(
'name'=>'operator.username',
'filter'=>CHtml::activeTextField($dataprovider, 'operator'),
),
array(
'name'=>'status',
'value'=>array('MailHelper', 'getEmailStatus'),
'filter'=> CHtml::activeDropDownList($dataprovider, 'status', Mail::getEmailStatuses()),
),
array(
'class'=>'CButtonColumn',
'template'=>'{update}',
'buttons'=>array(
'update' => array(
'url'=>'$this->grid->controller->createUrl("/email/editTemplate", array("templateId"=>$data->id))',
),
),
)
),
)
));
?>
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'mails-grid',
'dataProvider'=>$dataprovider->search(),
'columns' => $dialog->columns(),
'filter' => $dataprovider,
'template' => $dialog->link()."{summary}\n{items}\n{pager}",
)); ?>
I have Restaurant, City, Country and User models with relations between them.
Model:
public function search() {
$criteria=new CDbCriteria;
$criteria->together = true;
$criteria->with= array('xCountry','xCity','User');
$criteria->compare('Id',$this->Id,true);
$criteria->compare('Restaurant.Name',$this->Name,true);
$criteria->addSearchCondition('xCountry.Name',$this->Country);
$criteria->addSearchCondition('xCity.Name',$this->City);
$criteria->compare('Zip',$this->Zip,true);
$criteria->compare('Address',$this->Address,true);
$criteria->compare('Description',$this->Description,true);
$criteria->compare('Restaurant.Active',$this->Active,true);
$criteria->addSearchCondition('User.Username',$this->Owner);
$criteria->compare('Lat',$this->Lat);
$criteria->compare('Lon',$this->Lon);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
View:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'restaurant-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'Id',
'Name',
'Zip',
'Address',
'Active',
array(
'name' => 'User.Username',
'header' => 'Username',
'filter' => CHtml::activeTextField($model, 'Owner'),
'value' => '$data->User->Username',
),
array(
'name' => 'xCountry.Name',
'header' => 'Country',
'filter' => CHtml::activeTextField($model, 'Country'),
'value' => '$data->xCountry->Name',
),
array(
'name' => 'xCity.Name',
'header' => 'City',
'filter' => CHtml::activeTextField($model, 'City'),
'value' => '$data->xCity->Name',
),
array(
'class'=>'CButtonColumn',
),
),
));
I hope this can help you.
UPDATE:
What if you try something like this:
...
'columns'=>array(
'mailTemplate.name',
'sendDate',
'mailTemplate.subject',
'client.email',
...
UPDATE #2:
Prepare yourself this will be a bit dirty.
Let's say we've got two classes, A and B. B belongs to A.
B's got a property, let's say "color" and we want to display it in our grid where we list the "A"s.
The first thing you have to do is, manually create a property to your data provider class (what is "A").
This property will be "colorOfB", so you have to add "public $colorOfB;" to your model A.
Add criteria for this property:
$criteria->compare('B.color',$this->colorOfB,true);
Add the column to the grid:
array(
'name' => 'B.color',
'header' => 'Color of B',
'filter' => CHtml::activeTextField($model, 'colorOfB'),
'value' => '$data->B->color'),
The final thing is to set this property manually in A's controller:
$modelA = new A('search');
$modelA->colorOfB = $_GET['A']['colorOfB'];
this will set select list
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'avto-ugon-grid',
'dataProvider'=>$model_data,
'filter'=>$model,
'columns'=>array(
'id',
array(
'name' => 'time',
'value' => 'date("d/m/Y", $data->time)',
'type' => 'html',
),
array(
'name' => 'nomer',
'value' => '$data->nomer',
'type' => 'html',
),
array(
'name' => 'id_marka',
'value' => '$data->idMarka->mark',
'type' => 'html',
'filter'=> CHtml::listData(AvtoUgon::model()->with('idMarka')->findAll(array('group'=> 'id_marka', 'order'=> 'idMarka.mark')), 'id_marka', 'idMarka.mark'),
),
array(
'name' => 'id_model',
'value' => '$data->idModel->model',
'type' => 'html',
'filter'=> CHtml::listData(AvtoUgon::model()->with('idModel')->findAll(array('group'=> 'id_model', 'order'=> 'idModel.model')), 'id_model', 'idModel.model'),
),
array(
'name' => 'color',
'value' => $data->color,
'type' => 'raw',
),
array(
'name' => 'id_street',
'value' => '$data->idStreet->street',
'type' => 'html',
),
array(
'name' => 'publish',
'value' => 'CHtml::link($data->publish ? "Опубликовано" : "Не опубликовано", Yii::app()->controller->createUrl("publish", array("id" => $data->id)))',
'type' => 'html',
),
/*'id_street',
'nomer',
*/
array(
'class'=>'CButtonColumn',
),
),
));

Categories