Question

I joined some tables in model by CDbCriteria my code is something like this :

$crt = new CDbCriteria();
        $crt->alias = 'so';
        $crt->select = 'u.id, u.first_name, u.last_name';
        $crt->join = " inner join " . Flow::model()->tableName() . " as fl on fl.id = so.flow_id";
        $crt->join .= " inner join " . RoleUser::model()->tableName() . " as ru on ru.id = fl.receiver_role_user_id";
        $crt->join .= " inner join " . User::model()->tableName() . " as u on ru.user_id= u.id";
        $crt->compare('sms_outbox_group_id', $smsOutboxGroupId);
        $crt->compare('fl.kind', Flow::KIND_SMS);
        $crt->group = 'u.id';

        $smsOutBox = new SmsOutbox();
        return new CActiveDataProvider($smsOutBox, array(
            'criteria' => $crt,
            'sort' => array(
                'defaultOrder' => 'so.id DESC',
            )
        ));

how can I show my selected column in CGridView? is there any possible way to show first_name and last_name without defining relation in model?

Was it helpful?

Solution

I've found the solution using Sudhanshu Saxena answer. beside using aliases for first_name and last_name I've added two property to model with same names as aliases : receiverFirstName and receiverLastName. in this way my problem was solved and besides that it provide search functionality on this two property. my final code is like this :

Model :
public $receiverFirstName;
public $receiverLastName;

creating criteria :

$crt = new CDbCriteria();
        $crt->alias = 'so';
        $crt->select = 'so.id,u.id as userId, u.first_name as receiverFirstName, u.last_name as receiverLastName, so.status';
        $crt->join = " inner join " . Flow::model()->tableName() . " as fl on fl.id = so.flow_id";
        $crt->join .= " inner join " . RoleUser::model()->tableName() . " as ru on ru.id = fl.receiver_role_user_id";
        $crt->join .= " inner join " . User::model()->tableName() . " as u on ru.user_id= u.id";
        $crt->compare('sms_outbox_group_id', $smsOutboxGroupId);
        $crt->compare('u.first_name', $this->receiverFirstName, true);
        $crt->compare('u.last_name', $this->receiverLastName, true);
        $crt->compare('so.status', $this->status);
        $crt->compare('fl.kind', Flow::KIND_SMS);
        $crt->group = 'userId';
        $crt->order = 'so.id';


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

and finally in CgridView I did this :

'columns' => array(
    array(
        'header' => Yii::t('app', 'Row'),
        'value' => '$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)',
    ),
    array(
        'name' => 'receiverFirstName',
        'value' => '$data->receiverFirstName',
    ),
    array(
        'name' => 'receiverLastName',
        'value' => '$data->receiverLastName',
    ),
    array(
        'name' => 'status',
        'value' => 'SmsOutbox::getStatusTitle($data->status)',
        'filter' => CHtml::listData(SmsOutbox::getStatusList(), 'id', 'title')
    ),

),

OTHER TIPS

Use the alias and call it in CgridView like this

$crt->select = 'u.id, u.first_name as fname, u.last_name as lastname';

In your Grid call it.

array('name'=>'name' or 'header'=>'some header','value'=>'$data->fname')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top