如何关联多个加入 CDbCriteria Yii 1.1
How to relate more than one join CDbCriteria Yii 1.1
我在将 JOIN 中的 table 与 CDbCriteria
相关联时遇到问题。我让它与两个相关的 table 之间的单个 JOIN 一起工作,但我有以下情况:
table 的结构与要关联的字段:
table Tbl_recibo table tbl_domicilio table tbl_entidad
Idrecibo Iddomicilio Identidad
nombre nombre nombre
Iddomicilio Identidad
科迪戈 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`
Codigo 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']),
)));
}
关于 table 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'),
);
}
我需要可视化相同的 SQL 数据,但使用 CDbCriteria
,我无法从 tblRecibo
访问 tblEntidad
table 的数据。
你需要这样组合 JOIN 子句:
$r->join = 'left join ' . $dx . ' DX on DX.idDomicilio = t.idDomicilio ';
. 'left join ' . $ex . ' EX on EX.idEntidad=DX.idEntidad';
在您的代码中,您只是覆盖了对 $r->join
的第一个赋值。
您也可以考虑使用 with()
, so you could reuse relations definitions to build JOIN conditions. See this guide article about relations。
我在将 JOIN 中的 table 与 CDbCriteria
相关联时遇到问题。我让它与两个相关的 table 之间的单个 JOIN 一起工作,但我有以下情况:
table 的结构与要关联的字段:
table Tbl_recibo table tbl_domicilio table tbl_entidad
Idrecibo Iddomicilio Identidad
nombre nombre nombre
Iddomicilio Identidad
科迪戈 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`
Codigo 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']),
)));
}
关于 table 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'),
);
}
我需要可视化相同的 SQL 数据,但使用 CDbCriteria
,我无法从 tblRecibo
访问 tblEntidad
table 的数据。
你需要这样组合 JOIN 子句:
$r->join = 'left join ' . $dx . ' DX on DX.idDomicilio = t.idDomicilio ';
. 'left join ' . $ex . ' EX on EX.idEntidad=DX.idEntidad';
在您的代码中,您只是覆盖了对 $r->join
的第一个赋值。
您也可以考虑使用 with()
, so you could reuse relations definitions to build JOIN conditions. See this guide article about relations。