Search code examples
phpyiidataprovider

showing columns from joined tables in CGridView


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?


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')
        ),
    
    ),