Does Yii-CGridView column link works when dataProvider used with CSqlDataProvider? - yii

Code I used in CGridView gives me a null value in $data->gen_id and $data->erc_gen at the column value, when I inspect with the browser inspector it gives me
ercgen/admin code hereunder:
$qryTmp =
"SELECT gen_id, erc_ref,B.complete_name sales_executive,C.description type,D.client_name client_id,event_name, E.status_desc erc_status ".
"FROM erc_gen A, Users B, erc_type C, clients D, erc_status E ".
"WHERE A.sales_executive = b.user_id ".
"AND A.type = C.typ_no ".
"AND A.client_id = D.client_id ".
"AND A.erc_status = E.id ".
"AND sales_executive = '$user_id' ".
"AND erc_status = '1' ";
$count = Yii::app()->db->createCommand('SELECT COUNT(*) FROM ('.$qryTmp.') X')->queryScalar();
$dataProvider=new CSqlDataProvider($qryTmp, array(
'totalItemCount'=>$count,
'sort'=>array(
'attributes'=>array(
'gen_id','erc_ref', 'sales_executive', 'type','client_id','event_name','erc_status'
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
//CGRIDVIEW
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'pager'=>array(
'header'=>'',
'firstPageLabel'=>'<<',
'prevPageLabel'=>'<',
'nextPageLabel'=>'>',
'lastPageLabel'=>'>>',
),
'columns'=>array(
array(
'header'=>'#',
'value'=>'$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
),
array(
'header'=>'Reference Number',
'name'=>'erc_ref',
'type'=>'raw',
'value'=>'CHtml::link($data->erc_ref,Yii::app()->createUrl("ercGen/update",array("id"=>"$data->gen_id")))',
),

I found my answer here
Simply changing $data->erc_ref to $data['erc_ref'] and $data->gen_id to $data['gen_id']

Related

Displaying value from join table in Yii CgridView

I have this very simple table in my view and I'm trying to display value from joined table:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'skills-grid',
'dataProvider'=>$model->searchWithStudentSuccessRate($id),
'template' => '{items}{pager}',
'cssFile'=>Yii::app()->request->baseUrl. '/themes/'. Yii::app()->theme->name.'/css/table.css',
'htmlOptions'=>array('class'=>'datagrid', 'style'=>'width:550px;'),
'columns'=>array(
array(
'name'=>'name',
),
array(
'name' => 'value',
'header' => Yii::t('MainTrans', 'Value'),
'value' => '$data->student_skills->value',
),
array(
'name' => 'successRate',
'header' => Yii::t('MainTrans', 'Success Rate'),
'value' => '$data->successRate."%"',
),
),
));
And this is the search function:
public function searchWithStudentSuccessRate($id)
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->name,true);
$criteria->compare('t.threshold',$this->threshold);
$criteria->with = array('student_skills');
$criteria->together = true;
$criteria->select = array('IFNULL(CASE WHEN ROUND((student_skills.value/t.threshold)*100,0) > 100 THEN 100 ELSE ROUND((student_skills.value/t.threshold)*100,0) END,0) as successRate','*');
$criteria->group = "t.name";
$criteria->condition = 'student_skills.student_id = '.$id;
$criteria->compare('successRate',$this->successRate);
$criteria->compare('student_skills.value',$this->value);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>25,
),
'sort'=>array(
'defaultOrder'=>array(
'successRate'=>CSort::SORT_DESC,
),
'attributes'=>array(
'successRate'=>array(
'asc'=>'successRate',
'desc'=>'successRate DESC',
),
'*',
),
),
));
}
But I get error: "Trying to get property of non-object" when I added value column to my CGridView.
Everything works fine without column value (columns successRate and name are fine). Relation should be fine as well. The error I get means that the value doesn't exist but it should since I did something similar in my other views and there it works.
Can anyone tell what's wrong? I'm sure it's something minor but I'm struggling with this embarrasing problem for a while.
Thanks
It means that in some conditions $data->student_skills is NULL. Try change this:
'value' => '$data->student_skills->value',
to this
'value' => 'empty($data->student_skills) ? null : $data->student_skills->value',

count and sort in cgridview getValidators error

I'm getting an error when i add in STAT. I'm trying to count the number of responses for each user and sort it from desc order.
My error: CActiveDataProvider and its behaviors do not have a method or closure named "getValidators".
relation:
'stickerCount'=>array(self::STAT, 'UserSticker', 'user_id'),
controller:
$likes=new CActiveDataProvider('UserSticker');
$this->render('index',array(
'likes'=>$likes,
'sort'=>array(
'defaultOrder'=>'stickerCount DESC',
),
));
<?php
$this->widget ( 'bootstrap.widgets.TbGridView', array (
'id'=>'follower',
'type'=>'striped condensed',
'dataProvider'=>$likes,
'filter'=>$likes,
'columns'=> array(
'stickerCount',
)));
?>
try this:
$criteria = new CDbCriteria();
$criteria->join = 'JOIN (SELECT user_id, count(*) As stickerCounter '
. 'FROM user_sticker GROUP BY user_id) userSticker '
. 'ON userSticker.user_id = t.id';
$likes = new CActiveDataProvider('MainTable',array(
'criteria'=>$criteria,
));
...

extendedSummary Yiiboster not show the sum total of gridview

I have gridview using TbExtendedGridView yii booster, in footer gridview i want show the sum of field using extendedSummary, i have follow the tutorial in yiibooster web but i can't show the extendedSummary to show the sum of column. Pls help me what's wrong in my code below ?. in the footer just show the box with blank text.
//this isi my gridview code
$no_loan= $_GET[no_loan];
$sql2="SELECT * from tbangsuran where nomor_pinjaman = '$no_loan' and status_bayar=1 order by no ASC";
$sqlProvider = new CSqlDataProvider($sql2);
$this->widget('bootstrap.widgets.TbExtendedGridView', array(
//'filter'=>$model,
'id'=>'tbangsuran-grid',
'type'=>'striped bordered',
'template' => "{items}\n{extendedSummary}",
'dataProvider' =>$sqlProvider,
'columns'=>array(
array(
'name'=>'Angsuran Ke',
'headerHtmlOptions'=>array('style'=>'text-align:center; width:90px;'),
'htmlOptions'=>array('style'=>'text-align:center'
),
'value'=> '$data[\'no\']',
),
array(
'name'=>'Tanggal Tagihan',
'headerHtmlOptions'=>array('style'=>'text-align:center;width:90px;'),
'htmlOptions'=>array('style'=>'text-align:center'),
'value'=> 'date("d-m-Y",strtotime($data[\'tanggal_bayar\']))',
),
//'nomor_pinjaman',
array(
'name'=>'Tunggakan Pokok',
'headerHtmlOptions'=>array('style'=>'text-align:center; width:120px;'),
'htmlOptions'=>array('style'=>'text-align:right'),
'value'=> 'number_format($data[\'pastdue_pokok\'],0,"",".")',
),
array(
'name'=>'Tunggakan Bunga',
'headerHtmlOptions'=>array('style'=>'text-align:center; width:120px;'),
'htmlOptions'=>array('style'=>'text-align:right'),
'value'=> 'number_format($data[\'pastdue_bunga\'],0,"",".")',
),
array(
'name'=>'Total Tunggakan',
'headerHtmlOptions'=>array('style'=>'text-align:center;width:120px;'),
'htmlOptions'=>array('style'=>'text-align:right'),
'value'=> 'number_format($data[\'pastdue_pokok\']+$data[\'pastdue_bunga\'],0,"",".")',
),
),
'extendedSummary' => array(
'title' => 'Total Tunggakan',
'columns' => array(
'pastdue_pokok' => array('label'=>'Total Tunggakan','class'=>'TbSumOperation')
)
),
'extendedSummaryOptions' => array(
'class' => 'well pull-right',
'style' => 'width:300px'
),
));
You must use the same column name in extendedSummary field.
In your case would be:
'extendedSummary' => array(
'title' => 'Total Tunggakan',
'columns' => array(
'Total Tunggakan' => array('label'=>'Total Tunggakan','class'=>'TbSumOperation')
)
),
I recommend you to use short column name to this tasks.
Hope this helps!

converting sql query into cakephp format

Hi
i need the following sql query into cakephp find() format. the query it self is working fine but i need to change it.
$this->Part->query(
"SELECT `parts`.`id`,`parts`.`part_name`
FROM `parts`
LEFT JOIN (
SELECT `op` . *
FROM `order_parts` AS `op`
WHERE `op`.`order_id` =".$this->Session->read('orderid')."
) AS `vT`
ON ( `parts`.`id` = `vT`.`part_id` )
WHERE `vT`.`part_id` IS NULL"
);
thanks
If your relationship are Order HABTM Part and you have a table orders_parts with columns: id, order_id,part_id you should be able to do something like this:
First, get the ids of the parts which are in the order:
//each Part has one OrdersPart per order
$this->Part->bindModel(array('hasOne' => array('OrdersParts')));
$parts = $this->Part->find('list', array(
'fields' => array('Part.name'),
'conditions' => array(
'OrdersParts.order_id' => $this->Session->read('orderid'),
),
'recursive' => 2
));
Now get the parts which are not in the order:
$this->Part->find('all', array(
'conditions' => array(
"NOT" => array('Part.id' => array_keys($parts))
),
));

Conditions with And Or statements

I got this figured out. Here is the solution:
'conditions'=>array(
'OR' => array(
array('EavAttribute.attribute_code'=>'lastname'),
array('EavAttribute.attribute_code'=>'firstname')
),
'AND' => array(
array('UserEntityVarchar.entity_id'=>$id)
)
)
I am trying to convert this query into a cakephp query and I am having a bit of trouble with the conditions. No matter how I format the conditions, I always end up with the second query below.
Any help with this is greatly appreciated.
This is the query I am trying to replicate:
SELECT
`UserEntityVarchar`.`value_id`,
`UserEntityVarchar`.`attribute_id`,
`UserEntityVarchar`.`entity_id`,
`UserEntityVarchar`.`value`,
`EavAttribute`.`attribute_code`
FROM
`user_entity_varchars` AS `UserEntityVarchar`
LEFT JOIN `eav_attributes` AS `EavAttribute` ON(
`UserEntityVarchar`.`attribute_id` = `EavAttribute`.`attribute_id`)
WHERE
(UserEntityVarchar.entity_id = 1 AND
EavAttribute.attribute_code = 'firstname') OR
(UserEntityVarchar.entity_id = 1 AND
EavAttribute.attribute_code = 'lastname')
This is the query I keep getting no matter how I format my condition:
SELECT
`UserEntityVarchar`.`value_id`,
`UserEntityVarchar`.`attribute_id`,
`UserEntityVarchar`.`entity_id`,
`UserEntityVarchar`.`value`,
`EavAttribute`.`attribute_code`
FROM
`user_entity_varchars` AS `UserEntityVarchar`
LEFT JOIN `eav_attributes` AS `EavAttribute` ON(
`UserEntityVarchar`.`attribute_id` = `EavAttribute`.`attribute_id`)
WHERE
((`UserEntityVarchar`.`entity_id` = 1)
AND
(`EavAttribute`.`attribute_code` = 'firstname'))
AND
((`UserEntityVarchar`.`entity_id` = 1)
AND
(`EavAttribute`.`attribute_code` = 'lastname'))
This is the condition that I am using:
'conditions'=>array(
array(
array('UserEntityVarchar.entity_id'=>$id),
array('AND ' => array('EavAttribute.attribute_code'=>'firstname'))
),
array('OR' =>
array('UserEntityVarchar.entity_id'=>$id),
array('AND ' => array('EavAttribute.attribute_code'=>'firstname'))
)
)
You should be able to do it like this:
$this->Model->find('all', array('conditions' => array('OR' => array(
array('UserEntityVarchar.entity_id' => 1,
'EavAttribute.attribute_code' => 'firstname'),
array('UserEntityVarchar.entity_id' => 1,
'EavAttribute.attribute_code' => 'lastname')))));