简体   繁体   English

基于关系HAS_MANY数据的Yii CGridView构建表

[英]Yii CGridView build table base on relation HAS_MANY data

I have next tables: Questionnaire(main), Names, Address, Telephone. 我有下一个表格:问卷(主要),姓名,地址,电话。 Relation HAS_MANY (yes, i need different names). 关系HAS_MANY(是的,我需要使用其他名称)。

I'm trying to build in Questionnaire ActionView - > CGridView with data from Names. 我正在尝试使用“名称”中的数据在“调查表ActionView”->“ CGridView”中进行构建。

In Names table i have: 在名称表中,我有: 名称表 So i need CGridView which goes in "Questionnaire view" show all data from Names where id_questionnaire=$this->id. 因此,我需要在“问卷视图”中显示的CGridView显示名称中id_questionnaire = $ this-> id的所有数据。

But all that i could do by now, that is Names.name column show all in 1 row: AlexCorbenTest with $data->namesToString(); 但是我现在所能做的就是Names.name列在第一行中全部显示:AlexCorbenTest with $ data-> namesToString();

public function namesToString()
{
    $roles = $this->names;
    if($roles) {
        $string = '';
        foreach($roles as $role) {
            $string .= $role->name . ', ';
        }
        return substr($string,0,strlen($string)-1); // substr to remove trailing comma
    }
    return null;
}

$model->names->name show empty table with 1 row :( $ model-> names-> name显示带有1行的空表:(

My search() and $this->widget http://paste.ubuntu.com/6838820/ 我的search()和$ this-> widget http://paste.ubuntu.com/6838820/

Ok, i have answered myself, so guys if you need to get data from HAS_MANY relation in another table in CGRidView, just do not try to do it with relations (CActiveDataProvider) , use CSqlDataProvider instead. 好的,我已经回答了自己,所以如果您需要从CGRidView的另一个表中的HAS_MANY关系中获取数据,请不要尝试使用关系(CActiveDataProvider)进行操作,请改用CSqlDataProvider。

public function getLNames(){

    $count=Yii::app()->db->createCommand('SELECT COUNT(*) FROM tbl_names WHERE id_questionnaire='.$this->id)->queryScalar();
            $sql = '
                SELECT *  FROM tbl_names
                WHERE id_questionnaire='.$this->id;

            return new CSqlDataProvider($sql,array('keyField' => 'id_name','totalItemCount'=>$count,));
    }

View CGridView: 查看CGridView:

$this->widget('zii.widgets.grid.CGridView', array(
     'dataProvider'=>$model->getLNames(),

    'columns'=>array(

    array (
         'name'=>'names.name',
                'value'=>'$data[name]',
        ),

        array(
        'header'=>'Middle Name',
         'value'=>'$data[midname]',
                ),

        array(
        'header'=>'Last Name',
        'value'=>'$data[surname]',
        ),

    ),
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM