我在将JOIN中的表与CDbCriteria关联时遇到问题。我可以在两个相关的表之间使用单个JOIN来工作,但是有以下情况:
具有相关字段的表的结构:
SQL:
SELECT
`rec`.`idrecibo` AS 'Num_Recibo',
`dom`.`matricula`,
`dom`.`federado`,
`ent`.`nombre` AS 'profesional',
`dom`.`calle` AS 'domicilio'
FROM
`tbl_recibo` AS rec
LEFT JOIN `tbl_domicilio` AS dom ON `rec`.`iddomicilioapertura` = `dom`.`iddomicilio`
LEFT JOIN `tbl_entidad` AS ent ON `dom`.`identidad` = `ent`.`identidad`
yii:
public function traerRecibos() {
$r = new CDbCriteria();
$dx = TblDomicilio::model()->tableName();
$ex = TblEntidad::model()->tableName();
$r->select='t.idRecibo,DX.idDomicilio';
$r->join = 'left join ' . $dx . ' DX on DX.idDomicilio = t.idDomicilio';
$r->join = 'left join ' . $ex . ' EX on EX.idEntidad=DX.idEntidad';
return new CActiveDataProvider($this, array(
'criteria' => $r,
'pagination' => array('pageSize' => Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']),
)));
}
关系表tblRecibo:
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(
'tblEstadoreciboHasTblRecibos' => array(self::HAS_MANY, 'TblEstadoreciboHasTblRecibo', 'idRecibo'),
'tblItemrecibowebs' => array(self::HAS_MANY, 'TblItemreciboweb', 'idRecibo'),
'idCalendario0' => array(self::BELONGS_TO, 'TblCalendario', 'idCalendario'),
'idDomicilioApertura0' => array(self::BELONGS_TO, 'TblDomicilio', 'idDomicilioApertura'),
'idDomicilio0' => array(self::BELONGS_TO, 'TblDomicilio', 'idDomicilio'),
'idPaquete0' => array(self::BELONGS_TO, 'TblPaquete', 'idPaquete'),
);
}
查看索引tblRecibo模型:
$this->widget('bootstrap.widgets.TbGridView', array(
'type' => 'bordered striped',
'id' => 'tbl-recibo-grid',
'dataProvider' => $dataProvider,
'htmlOptions' => array('style' => 'font-size: 16px;font-weight: normal'),
'columns' => array(
array(
'name' => 'idRecibo',
'header' => 'N°Recibo',
'headerHtmlOptions' => array('style' => 'width: 10px'),
),
array(
'name' => 'idDomicilio',
'header' => 'Matricula',
'value' => 'isset($data->idDomicilio0->matricula)?$data->idDomicilio0->matricula:""',
'headerHtmlOptions' => array('style' => 'width: 15%'),
// 'filter' => CHtml::listData(TblDomicilio::model()->findAll('idDomicilio', 'nombre'),
),
));
?>
我需要在视图中显示与tblRecibo没有直接关系的tblEntity表的“名称”字段的值。