我有两个表,表名和字段列在下面
company(tbl_id,sales_id,user_id,assigned_on)
users(user_id,user_name)
我想执行以下查询
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
我有一个型号Company.php我定义了以下关系
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'))
);
}
并在相应的函数中编写了代码
$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;
我通过了提供者来查看在视图文件中我编写了代码
<?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'),
)
)
));
但是第二个表的字段(user_name)的值未显示在列表中。我经常搜索并尝试很多选项。任何人都可以让我知道这个问题的解决方案 提前致谢
答案 0 :(得分:0)
'value'=>'$data->company->user_name'
,
应该是
'value'=>'$data->company->user->user_name',
公司没有user_name。