Yii: Can not list data in Grid View - php

I cant list data in grid using yii framework.My controller is Sitecontroller.php,My view is list_jobseeker.php .I got the error "Fatal error: Call to a member function getData() on a non-object ".Anybody help me?
My controller code
public function actionlist_jobseeker()
{
$session_id=Yii::app()->session['user_id'];
if ($session_id == "")
{
$this->redirect( array('/employee/site/login'));
}
$user_id =$session_id;
$models = 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('models' =>$models));
}
View- 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($models),
'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"]))'),
)
)
),
));
?>

dataProvider should be a CDataProvider instance. You are passing in an array: $model. You could wrap this in a CArrayDataProvider:
'dataProvider' => new CArrayDataProvider($model),
A couple of other issues:
a) Your gridview expects $data to be an object and not an array. Either alter all $data->* instances to $data[*] or use CActiveDataProvider and the CActiveRecord instance for the job_seeker_profile table
b) By convention $model usually refers to a single CModel instance. Your array should therefore be named something else in plural e.g $items

I use this:
$data = ...::model()->findAll();
$dataProvider=new CArrayDataProvider('Class of your object');
$dataProvider->setData($data);
$dataProvider->keyField = "primary key of your model";
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
...
array( 'class'=>'CButtonColumn',
'template' => '{ver}',
'buttons' => array(
'ver' => array(
'url'=>'"?id=".$data["..."]',
'imageUrl'=> Yii::app()->baseUrl.'/images/view.png', //Image URL of the button.
),
)
),

Related

Filter not working in Yii CGridView (Yii 1.x)

i checked 1000 times my code
all things okey ! but after i change my theme ! its not send ajax request for filtering grid view
when i checked this code i thought is okey and i should say that i searched and check wiki yii but i couldnt find my problem
my view Code:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'product-grid',
'itemsCssClass' => 'table table-bordered table-striped dataTable',
'dataProvider' => $model->notDeleted()->search(),
'filter' => $model,
'columns' => array(
'appId',
array(
'name' => 'categoryId',
'value' => '$data->category->name',
'filter' => CHtml::activeDropDownList($model, 'categoryId', CHtml::listData(Category::model()->notDeleted()->findAll(), 'id', 'name'),
array('empty' => '')
)
),
'name',
'packageName',
'price',
'discount',
'version',
array(
'name' => 'isActive',
'value' => '$data->isActive ? Yii::t("app", "Yes") : Yii::t("app", "No")',
'filter' => CHtml::activeDropDownList($model, 'isActive', array(
1 => Yii::t('app', 'Yes'),
0 => Yii::t('app', 'No'),
))
),
array(
'name' => 'inAppPurchase',
'value' => '$data->inAppPurchase ? Yii::t("app", "Yes") : Yii::t("app", "No")',
'filter' => CHtml::activeDropDownList($model, 'inAppPurchase', array(
1 => Yii::t('app', 'Yes'),
0 => Yii::t('app', 'No'),
))
),
/*
'createUserId',
'createTime',
'isDeleted',
*/
array(
'class' => 'CButtonColumn',
),
),
)); ?>
and my controller:
$model = new Product('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['Product']))
$model->attributes = $_GET['Product'];
$this->render('admin', array(
'model' => $model,
));
please help me !
For the next one, I had the same problem. I proceeded like this and it worked. for my case I delete
<script src="<?php echo Yii::app()->theme->baseUrl; ?>/js/jquery-1.11.1.min.js">
</script>
Check if you included jquery.js more than once in your page. I had the same error and the reason was that yii had already added the jquery.js script, so no need to include it yourself.
Cr.Andrey Zausaylov
https://stackoverflow.com/a/41304449/3032180

CArrayDataProvider in YII sorting not working on some fields

I have created Dataprovider in controller and pass it to view file. It display correct Data. There are total 7 columns to display.
But only on 2 fields sorting is not working and throwing ERROR-500.
In controller File, my Code is :
$OutboundRatecardDetails_data = OutboundRatecardDetails::model()->findAllByAttributes(array('orc_id'=>$orc_id));
$pageSize = 10;
if(isset($_GET['pageSize']))
{
$pageSize = $_GET['pageSize'];
Yii::app()->user->setState('pageSize', (int)$_GET['pageSize']);
unset($_GET['pageSize']);
}
$dataProvider=new CArrayDataProvider($OutboundRatecardDetails_data, array(
'id'=>'orcd_id',
'sort'=>array(
'attributes'=>array(
'orcd_id', 'des_id', 'orcd_duration','orcd_rate','orcd_grace_duration','orcd_description'
),
),
'pagination'=>array(
'pageSize'=>$pageSize,
),
'keyField'=>'orcd_id',
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
In admin.php view file, my code is :
<?php $this->widget('application.components.widgets.tlbExcelView', array(
'id'=>'Outbound-rate-card-details-grid',
'dataProvider' =>$dataProvider,
'itemsCssClass' =>'table table-bordered table-striped table-condensed',
'pagerCssClass' =>'dataTables_paginate paging_bootstrap pagination',
'htmlOptions' =>array('class'=>'adv-table'),
'columns'=>array(
'des_id'=>array(
'name'=>'des_id',
'header'=>'Destination',
'value' => '(!empty($data->rel_destination->des_prefix) ? $data->rel_destination->des_prefix : "")',
),
array(
'name'=>'orcd_grace_duration',
'header'=>'Grace Duration',
'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
),
array(
'name'=>'orcd_duration',
'header'=>'Incr. Duration',
'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
),
array(
'name'=>'orcd_rate',
'header'=>'Incr. Rate',
'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
),
array(
'name'=>'orcd_min_duration',
'header'=>'Min. Duration',
'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
),
array(
'name'=>'orcd_min_rate',
'header'=>'Min. Rate',
'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
),
array(
'name'=>'orcd_description',
'header'=>'Rate Description',
'htmlOptions' => array('style' => 'max-width:150px; white-space:normal;word-break:break-all;'),
),
),
)); ?>
When I try to sort on 2nd and 3rd field (orcd_grace_duration,orcd_duration), it throws error. Error screenshot is also attached. Other than these 2 fields, sorting is perfectly working.
This issue occurs when CArrayDataProvider's columns having same values.
Try using CActiveDataPrider instead.
You just need to change code in your controller like below:
$criteria = new CDbCriteria;
$criteria->compare('orc_id',$orc_id,true);
$dataProvider = new CActiveDataProvider('OutboundRatecardDetails', array(
'criteria'=>$criteria,
'sort' => array(
'defaultOrder' => 'orc_id desc',
),
'pagination'=>array(
'pageSize'=>$pageSize,
),
));

Yii1 Detail View , how to add class to image

I am using yii1 and facing problem in adding class to image shown in detail view , tell me how to add css class or static property to image to make width and height 40px;
my detail view code is below
<?php
$this->widget('zii.widgets.CDetailView', array(
'data' => $model,
'attributes' => array(
'id',
'title',
array(
'label' => 'image',
'type' => 'image',
'value' => Yii::app()->baseUrl . '/images/foodcategory/'
.$model->image ,
),
'status',
'created_at',
'created_by',
'modified_at',
'modified_by',
),
));
?>
Change the type to raw. You can then add it using CHtml::image():
array(
'label' => 'image',
'type' => 'raw',
'value' => CHtml::image(
Yii::app()->baseUrl . '/images/foodcategory/'.$model->image,
'Alt text',
array('class' => 'myclass') //and other html tag attributes
),
),
[
'attribute'=>'image_url',
'type' => 'image',
'value'=> \Yii::$app->request->BaseUrl.'/'.$model->image_url,
'format'=> ['image',['width'=>'100','height'=>'100']],
],

change id checkboxs in CGridView

there are 2 columns check box in cgridViewtable and two bootstrap widgets TbButton in view page.
I can not get value of my checkbox very good. My value in checkboxs transfer into controller but changed id of checkboxs After a period of timeand , and controller don't knew checkbox,
View:
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id' => 'profile-information-form',
'enableAjaxValidation' => false,
));
$this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'submit', 'type' => 'primary', 'label' => Yii::t('fa_ir', 'First validation'),));
$this->widget('bootstrap.widgets.TbButton', array('buttonType' => 'submit', 'type' => 'primary', 'label' => Yii::t('fa_ir', 'End validation'),));
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'profile-information-grid',
'dataProvider' => $model->children(),
'filter' => $model,
'columns' => array(
array(
'header' => '',
'value' => '$this->grid->dataProvider->pagination->offset + $row+1', // row is zero based
),
array(
'name' => 'ProfileInformation.user.scope',
'value' => 'CHtml::encode($data->user->scope->name)',
'filter' => Scope::model()->options,
),
array(
'name' => 'id',
'value' => 'CHtml::encode($data->id)',
),
array(
'name' => 'center',
'value' => 'CHtml::encode($data->center)',
),
array(
'name' => 'sendCount',
'value' => 'CHtml::encode($data->sendCount)',
'filter' => '',
),
// Use CCheckbox column with selectableRows = 2 for Select All
array('class' => 'CCheckBoxColumn', 'selectableRows' => 2, 'visible' => (Lookup::isUser2(Yii::app()->user->id) or Lookup::isAdmin(Yii::app()->user->id))),
array('class' => 'CCheckBoxColumn', 'selectableRows' => 2, 'visible' => (Lookup::isUser1(Yii::app()->user->id) or Lookup::isAdmin(Yii::app()->user->id))),
),
));
// action button
$this->endWidget();
Controller:
if (isset($_POST['profile-information-grid_c10']) & isset($_POST['yt0'])) {
////Do action1
}
if (isset($_POST['profile-information-grid_c12']) & isset($_POST['yt1'])) {
////Do action2
}
}
my problem is in $_POST['profile-information-grid_c10'] and _POST['profile-information-grid_c12'] , before id were $_POST['profile-information-grid_c8'] and $_POST['profile-information-grid_c10'] , and now is $_POST['profile-information-grid_c12'] and $_POST['profile-information-grid_c14'].
my problem is very involved. I cannot explain very good.
I want to have a fix id.
I donto why change id of check box?
Can I assign ids for checkboxs?
Can I assign ids for checkboxs? Sure, you better explicitly set checkbox column id:
'class' => 'CCheckBoxColumn', 'id'=>'column1', ... see these docs.
This way you firmly set it and you'll have no things like these yt0, yt2...
The same you can do for the buttons. See TButton docs.
Also you need to use double && in logical condition:
isset($_POST['profile-information-grid_c12']) & isset($_POST['yt1'])

Yii CDetailView translate sub-property

<?php
$this->widget('zii.widgets.CDetailView', array(
'data' => $model,
'htmlOptions' => array('style' => 'width:425px; float:left; margin-right:20px; word-break:break-all', 'class' => 'detail-view table table-striped table-condensed'),
'attributes' => array(
'id',
'name',
'street',
'housenumber',
'zipcode',
'city',
array(
'label' => Yii::t('api', 'Country'),
'name' => Yii::t('api', 'country.name'),
),
),
));
?>
I want to translate the values for the attributes label and name in the array for the columns.
But it only translate the label and not the name.
Can someone tell me, what I am doing wrong?
name is the column name in the database, I think you want to adjust value, try this:
'label' => Yii::t('api', 'Country'),
'name' => 'country.name', //in reality you don't need this since you are setting the value
'value' => Yii::t('api', $model->country->name),

Categories