Connecting Data From 2 Different Tables In 1 View

Good morning!

I already search something thrugh the forum but I didn’t find a solution!

I’m trying to connect 2 table in one unique view.

I have 2 tables, argomento(topic) and domande(questions), both there is an id that connect the 2 table id_argomento.

I would like showing the questions connected to a topic down in the same view.

I tried whith the following code.

I insered the relation in my model.




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(

          'id_argomento' => array(self::BELONGS_TO, 'Domande', 'id_argomento'),

        );

    }




Then I created a function search to print the rows(questions) connected to my topic with id_argomento




public function search01()

        {

               

                $connection=Yii::app()->db;     

                $sql="SELECT * FROM domande where id_argomento ='$data->id_argomento'";

                $command=$connection->createCommand($sql);

                $dataReader=$command->query();

                $rows=$dataReader->readAll();

                $ans=array();

                foreach($rows as $data)

                {

                        $ans=$data['domanda'];

                }

                return $ans;

        

        }



Then in the _view of my topic I inserted the following code




<?php $this->widget('zii.widgets.grid.CGridView', array(

        'dataProvider'=>$model->search01(),

        'filter'=>$model,

        'columns'=>array(

                'id_argomento',

                'domanda',

                array('header'=>'Domanda per questo argomento','value'=>array($model,'search01')),

                'id_argomento',

                array('class'=>'CButtonColumn',

                        'template'=>'{view}'),

                ),

)); ?>




I don’t understand why I got this error.

Fatal error: Call to a member function getData() on a non-object in C:\xampp\htdocs\yii\framework\zii\widgets\CBaseListView.php on line 108

Please help me, I’m new with yii and php.

I hope somebody can help me.

THANKS VERY MUCH!!!!

You have incorrect sql query

Change


 $sql="SELECT * FROM domande where id_argomento ='$data->id_argomento'";



To


 $sql="SELECT * FROM domande where id_argomento ='{$data->id_argomento}'";



It first error,what I found. But I think it not alone :)

You are making it too complex - things that you want is actually OTB solution contained in the framework.

You made few mistakes (not using Yii best practice and also calling $data from the model method cant work.

Simply do the following:

In model (Questions):




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(

          'topic' => array(self::BELONGS_TO, 'Domande', 'id_argomento'),

        );

    }




in the view




<?php $this->widget('zii.widgets.grid.CGridView', array(

        'dataProvider'=>$model->search(),

        'filter'=>$model,

        'columns'=>array(

                'id_argomento',

                'domanda',

                'topic.name_of_the_column_you_want_to_display_from_the_topic_table',

)); ?>




Hi,

Maybe my answer for this topic will not resolve your problem directly… but I would recommend (if your db engine supports this) you to create view on db side and then use this view as model in your application - it is much better solution from performance point of view… and a bit easier for future use I believe.

BR

Thanks to everybody for the advices!

I’m trying with another code.

I set the relations

In model Argomento.php (topic)




public function relations()

{

    return array(

        'domanda'=>array(self::BELONGS_TO,'Domande','id_argomento'),

    );

}



Then in Domande.php (Question)




   public function relations()

{					{

    return array(

        'argomento'=>array(self::HAS_MANY,'Argomento','id_argomento'),

		 

    );

}



At the end I inserted this code in the view of Argomento




$this->widget('zii.widgets.grid.CGridView', array(


    'dataProvider'=>new CActiveDataProvider('Domande',array(

        'criteria'=>array(

            'with'=>array( 

                'argomento'=>array(

                    'select'=> 'id_argomento',

					

                ),

            ),

        ),

    )),

));



In this way I have all the question printed in one topic but I would like to print only the question rows related to my topic with the field id_argomento.

What can I do?

Thanks everybody!

Somebody know a different solution?

Please Help!!

Thanks guys.

Check this:

http://www.yiiframework.com/doc/guide/1.1/en/database.ar

You have to use find() method and switch model.

BR

@Dino : I was looking for solution to a similar problem and your post did help me move a step further. Thanks !