简体   繁体   中英

Writting query using CDbcriteria for CgridView in YII.?

I am trying to show my data in gridview and in process have written the following query which I am trying to write using CDBCriteria ,

//query

SELECT user. * , jobs. * , COUNT( jobs.user_id ) 
FROM user
INNER JOIN jobs ON user.id = jobs.user_id
GROUP BY user.id

I have tried the following thing:

$criteria = new CDbCriteria;
$criteria->select ='user.*,jobs.*';
$criteria->select ='COUNT(jobs.user_id)';
$criteria->select ='user';
$criteria->join   ='INNER JOIN jobs ON user.id = jobs.user_id';
$criteria->group  ='user.id';

return new CActiveDataProvider('manageemployers', array(
    'criteria'=>$criteria,

my view have the following code.

 <?php
 $this->widget('zii.widgets.grid.CGridView', array(
     'dataProvider' =>$model->search(),
     //   'filter' => $model,
     'columns' => array(
         array(
             'name' => ' Employer ID',
             'type' => 'raw',
             'value' => 'CHtml::encode($data->id)',
             'htmlOptions' => array('style'=>'width:90px;','class'=>'zzz'),
             // 'filter'=>'false' /* for hiding filter boxes */

         [...]

?>

My contoller

public function actionManageEmployers() {

    $user_id = Yii::app()->session['user_id'];
    if (Yii::app()->user->getId() === null)
        $this->redirect(array('site/login'));
    $model = new ManageEmployers();
    $model->user_id = $user_id;


    $this->render('manageemployers', array('model' => $model, 
    ));
}

But its not working. Please help me on this one. Thanks!

EDIT

So now I'm able to understand your problem and I tried to fix it with this.

i think i had the solution, you don't need to get the data in the search() like you did it. you can use relations and other stuff to get your data.

firstly i would add a relation to your user-model like this:

'jobs' => array(self::HAS_MANY, 'Jobs', 'user_id')

after that, you can access all jobs of the user with $model->jobs

then you need to undo your changes of the search() -function. I mean that it must look like this:

public function search()
{
    $criteria=new CDbCriteria;

    $criteria->compare('attribute',$this->attribute);
    //this is just an example ^

    return new CActiveDataProvider($this, array(
        'criteria'=>$criteria,
    ));
}

changes in the action :

$model = new ManageEmployers();

to this:

$model = new ManageEmployers("search");

changes in the view :

add something like this to your colums array:

array( 
    'name' => 'Total Jobs Posted',  
    'value' => 'count($data->jobs)', 
    'htmlOptions' => array('style'=>'width:90px;','class'=>'zzz'), 
)

If you have table relations set up, then in your User model (that refers to your user table) try writing your CDbCriteria like so:

$criteria = new CDbCriteria();
$criteria->with = array('jobs' => array('joinType' => 'STRAIGHT_JOIN'));
$criteria->together = true;
$criteria->addCondition('id = jobs.user_id');

return new CActiveDataProvider('manageemployers', array(
  'criteria' => $criteria
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM