0
votes

I have next tables: Questionnaire(main), Names, Address, Telephone. Relation HAS_MANY (yes, i need different names).

I'm trying to build in Questionnaire ActionView - > CGridView with data from Names.

In Names table i have: Names table So i need CGridView which goes in "Questionnaire view" show all data from Names where 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();

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 :(

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

1
did you do print_r($this->names);? Are you getting data in $roles? - Kumar V
Yes i did, here results: CVarDumper::dump($model->search(),50,true); -paste.ubuntu.com/6839114 CVarDumper::dump($model->names,50,true);- paste.ubuntu.com/6839122 Yes i get data, but it just 1 row string in result table. When i need 3 rows for each result, same as Mysql table looks like. - KorbenDallas

1 Answers

1
votes

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.

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:

$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]',
        ),

    ),
));