0

I have two tables, tables names and fields are listed below

company(tbl_id,sales_id,user_id,assigned_on)
users(user_id,user_name)

I want to execute the following query

SELECT assigned_on,u.user_name FROM company c LEFT JOIN users u ON c.user_id=u.user_id WHERE c.`sales_id`=2

I have one model Company.php I had defined the following relation

 public function relations()
        {
            // NOTE: you may need to adjust the relation name and the related
            // class name for the relations automatically generated below.
            return array(
            'company'=>array(self::HAS_MANY,'Users',array('user_id'=>'user_id'))

            );
        }

and in the corresponding function I wrote the code

$criteria=new CDbCriteria;
$criteria->select   =   "user_id,assigned_on";
$criteria->compare('t.sales_id',$salePersonID);
            $criteria->with=array(
                'company'
            );
            $criteria->together = true;
$provider   =    new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
            'pagination'=>array('pageSize'=>'25')
        ));
return $provider;

I passed the provider to view In the view file I wrote the code

<?php $this->widget('bootstrap.widgets.TbGridView',array(
    'type'=>'striped bordered condensed',
    'id'=>'users-grid',
    'dataProvider'=>$provider,  
    'template'=>"<table style='width: 100%'><tr><td>{pager}</td><td style='vertical-align:bottom;text-align: right;'>{summary}</td></table>\n{items}\n{pager}",
    'columns'=>array(
        array(
            'name'=>'assigned_on',          
            'header'=>'Assigned On',
            'htmlOptions'=>array('class'=>'cname'),
        ),      
        array(
            'name'=>'user_name',
            'value'=>'$data->company->user_name',           
            'header'=>'Contact Person',
            'htmlOptions'=>array('class'=>'cname'),
        )
    )
)); 

But the values of the second table's field(user_name) is not showing in the listing. I searched a lot and try many options. Can anybody please let me know the solution of this problem Thanks in advance

Leo
  • 31
  • 8
  • I think there is a mistake in your sql: "SELECT assigned_on". You should use table name alias to extract assigned_on column: "SELECT c.assigned_on". This comment does not relate to the whole question - just to this sql piece. – Alex Oct 21 '15 at 14:35

1 Answers1

0

'value'=>'$data->company->user_name',

should be

'value'=>'$data->company->user->user_name',

company does not have a user_name.

Jeroen
  • 579
  • 5
  • 19