我从版本1.1的世界Yii框架开始,对我来说,无法可视化纯查询带来的记录。我无法解决问题已经有好几天了,这可能很简单,但是由于我的知识不足,我还无法解决它。我向您展示我的代码,看看您是否可以帮助我。
我想在其中进行查询的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'),
);
}
我的模特:
public function search()
{
$criteria = new CDbCriteria;
$criteria->alias = 'r';
$criteria->select = 'r.idRecibo,d.matricula,d.federado,e.nombre,d.calle,r.cantidad,cal.periodo,est.ult_modif,os.nombre ';
$criteria->join ='LEFT JOIN tbl_calendario as cal on r.idcalendario=cal.idcalendario '
.'LEFT JOIN tbl_estadorecibo_has_tbl_recibo as est on r.idrecibo=est.idrecibo '
.'LEFT JOIN tbl_domicilio as d ON r.iddomicilioapertura=d.idDomicilio '
.'LEFT JOIN tbl_entidad as e ON d.idEntidad=e.idEntidad '
.'LEFT JOIN tbl_itemreciboweb as rweb on r.idrecibo=rweb.idrecibo '
.'LEFT JOIN tbl_ooss as os on rweb.idooss=os.idooss';
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array('pageSize' => Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']),
),
));
}
我的控制器:
public function actionIndex()
{
$model = new TblRecibo('search');
$model->unsetAttributes(); // clear any default values
if (isset($_GET['TblRecibo']))
$model->attributes = $_GET['TblRecibo'];
$this->render('admin', array(
'model' => $model,
));
}
我的观点:
<?php
$this->widget('bootstrap.widgets.TbGridView', array(
'type'=>'bordered striped',
'id' => 'tbl-recibo-grid',
'dataProvider' => $model->search(),
'htmlOptions' =>array('style' => 'font-size: 16px;font-weight: normal'),
'columns' => array(
'idrecibo',
'dom.matricula',
'dom.federado',
'ent.nombre',
'dom.calle',
'rec.cantidad',
'cal.periodo',
'est.ult_modif',
'os.nombre',
),
));
?>
执行时,我没有错误,只是GridView
没有显示查询结果。我知道应该没有什么复杂的,因为调试值是我不知道如何将其传递给Vista的问题。
答案 0 :(得分:0)
使用r.idRecibo,d.matricula,d.federado, ...
之类的select时,将返回诸如idRecibo
,matricula
,federado
等键的结果。要处理此类查询,您需要添加联接表中的所有字段到您的模型:
TblRecibo extends CActiveRecord {
public $matricula;
public $federado;
// ...
}
然后在网格视图中通过以下方式访问它们:
'columns' => array(
'idRecibo',
'matricula',
'federado',
// ...
),
但是对于此类自定义查询,使用CSqlDataProvider
而不是CActiveDataProvider
可能会更容易-您将无需为该搜索添加模型属性-结果将以数组形式返回:>
public function search() {
$from = 'FROM tbl_recibo as r '
. 'LEFT JOIN tbl_calendario as cal on r.idcalendario=cal.idcalendario '
. 'LEFT JOIN tbl_estadorecibo_has_tbl_recibo as est on r.idrecibo=est.idrecibo '
. 'LEFT JOIN tbl_domicilio as d ON r.iddomicilioapertura=d.idDomicilio '
. 'LEFT JOIN tbl_entidad as e ON d.idEntidad=e.idEntidad '
. 'LEFT JOIN tbl_itemreciboweb as rweb on r.idrecibo=rweb.idrecibo '
. 'LEFT JOIN tbl_ooss as os on rweb.idooss=os.idooss';
$count = Yii::app()->db->createCommand('SELECT COUNT(*) ' . $from)->queryScalar();
$sql = 'SELECT r.idRecibo,d.matricula,d.federado,e.nombre,d.calle,r.cantidad,cal.periodo,est.ult_modif,os.nombre ' . $from;
return new CSqlDataProvider($sql, array(
'totalItemCount' => $count,
'pagination' => array(
'pageSize' => Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']),
),
));
}