Yii Framework Forum: Get Value And Than Get Data For Another Table In Cgridview - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Get Value And Than Get Data For Another Table In Cgridview Rate Topic: -----

#1 User is offline   masdib.banget 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 02-July 13

Posted 14 October 2013 - 12:34 PM

Hi all, I have table
Status
id
kode
status

Punish
id
id_punish <-- FK
nilai
berlaku_punish

I have query like this :
SELECT status.kode, status.status, m1.berlaku_punish, m1.nilai
FROM STATUS
LEFT JOIN punish m1 ON status.id = m1.id_status
AND m1.id = (
SELECT MAX( m2.id )
FROM punish m2
WHERE m2.id_status = status.id ) 


I was googling.. for custom column cgridview with this condition, but not solved. :(
And my view for admin is : (this show data berlaku_punish and nilai but not with condition for max id)
<?php $this->widget('bootstrap.widgets.TbGridView', array(
	'id' => 'join-status-punish-grid',
	'dataProvider' => $model->search(),
        'type' => 'striped bordered condensed',
	'filter' => $model,
	'columns' => array(
		array(
                            'header' => 'Kode',
                            'name' => 'kode',
                            'htmlOptions'=>array('width'=>'100px'),
                    ),
		'status',
                'punishes.id_status', // i have relation in has_one
		'punishes.id_status',
		
		array(
			'class'=>'bootstrap.widgets.TbButtonColumn',
                        'template'=>'{tambah}  {detail}',
//                        'header'=>'T. Operasi',
                        'buttons'=>array(
                            'tambah' => array
                            (
                                'icon'=>'icon-plus-sign',
                                'url'=>'Yii::app()->createUrl("master/2/punish/create", array("id"=>$data->id))',
                            ),
                            
                            'detail' => array
                            (
                                'icon'=>'icon-time',
                                'url'=>'Yii::app()->createUrl("master/2/punish/create", array("id"=>$data->primaryKey))',
                            ),
                        ),
		),
	),
)); ?>


Any help for this???
0

#2 User is offline   mrk 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 33
  • Joined: 07-March 11

Posted 15 October 2013 - 05:39 AM

CActiveDataProvider which is by default returned from search() method of ActiveRecord entity doesn't support sub-queries (queries inside queries). But you can easily achieve what you want by using

http://www.yiiframew...SqlDataProvider

You can paste this SQL to the constructor of this object and after slight changes this should work.

Ask if you still have problems with that.
0

#3 User is offline   masdib.banget 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 63
  • Joined: 02-July 13

Posted 17 October 2013 - 02:43 AM

View Postmrk, on 15 October 2013 - 05:39 AM, said:

CActiveDataProvider which is by default returned from search() method of ActiveRecord entity doesn't support sub-queries (queries inside queries). But you can easily achieve what you want by using

http://www.yiiframew...SqlDataProvider

You can paste this SQL to the constructor of this object and after slight changes this should work.

Ask if you still have problems with that.

Yes, thanks
But i dont have to searching data.
        $connection=Yii::app()->db;
        $key="kode";
        $sql="SELECT status.id, status.kode, status.status, m1.berlaku_punish, m1.nilai
        FROM STATUS LEFT JOIN punish m1 ON status.id = m1.id_status
        AND m1.id = ( SELECT MAX( m2.id ) FROM punish m2 WHERE m2.id_status = status.id)";
        
        $rowcountsql = "SELECT COUNT(*) FROM status";
        $command = $connection->createCommand($rowcountsql);
        $count=$command->queryScalar(); // provide count for pagination
            // create data provider that works with CGridView
        $dataProvider=new CSqlDataProvider($sql, array(
        'keyField' => $key,
        'totalItemCount'=>$count,
        'pagination'=>array(
        'pageSize'=>500,
        ),
        ));
        $this->render('admin1',array('dataProvider'=>$dataProvider
        ));

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users