Need Help::getting Data From Other Model

Good day everyone I just want to how to display all data from other model.

In my situation… the 2 models are SurveyInfo and SurveyQuestions… I’d like to display all data of SurveyQuestions (ex. using CGridView) in SurveyInfo …/view/surveyinfo/view.php.

what things do I need to change or add in SurveyInfoController and SurveyInfoModel? Please help me. I’m just new in Yii.

A big thanks for those who will help me. God Bless and more power Yii masters. :D

in your surveyinfo controller




public function actionView() {  

    	$model = new SurveyQuestions('search');

    	$model->unsetAttributes();


    	$this->render('view',array(

        	'model'=>$model

    	));

}

/view/surveyinfo/view.php.


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

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

    'columns'=>array(    	

        'field1',

        'field2',

      ) 

));

thanks for the response sir. I’ll try that.

Welcome :)

let me know if you have any problem…

wow! it worked, thanks sir. :D

hmmm sir can I ask additional question?

about displaying the data… how can I filter the data that display in the views/surveyinfo/view.php

I just what to display only the survey questions with specific survey_id… where will I put the ‘where clause’ to filter the data? is it in surveyquestionmodel?

what things do I need to change/add?

you have to define relations in ur models rules function

and also primary key / foreign key relations in database

then you have to change in ur actionView function like this


$criteria  = new CDbCriteria();

    	$criteria->with = array('relation-name-with-other-model');

    	$model = new CActiveDataProvider('ModelName',array(

        	'criteria'=>$criteria

    	));

view.php


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

   'dataProvider'=>$model,

    'columns'=>array(           

        'field1',

        'field2',

      ) 

));

hmm what will I change in the rules function in surveyInfomodel? and sir. will i delete the ::

public function actionView() {

    $model = new SurveyQuestions('search');


    $model->unsetAttributes();





    $this->render('view',array(


            'model'=>$model


    ));

}

and change it to this ?

public function actionView() {

    $model = SurveyQuestions::model()->with('other-model-relation-name')->findAll();


    $this->render('view',array(


            'model'=>$model


    ));

}

SORRY SIR… IM JUST A NEWBIE. :(

paste ur both models (SurveyInfo.php and SurveyQuestions.php) relations functions here

for surveyInfo

public function rules()


{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('fcode, survey_name, survey_details', 'required'),


		array('survey_status', 'length', 'max'=>13),


		array('fcode', 'length', 'max'=>13),


		array('survey_name', 'length', 'max'=>100),


		array('survey_details', 'length', 'max'=>200),


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


		array('survey_id, fcode, survey_name, survey_details, survey_status', 'safe',                      'on'=>'search'),


	);


}

and for surveyQuestions ::

public function rules()


{


	// NOTE: you should only define rules for those attributes that


	// will receive user inputs.


	return array(


		array('survey_id, surveyQuestion_content, surveyQuestion_details', 'required'),


		array('survey_id', 'numerical', 'integerOnly'=>true),


		array('surveyQuestion_content', 'length', 'max'=>300),


		array('surveyQuestion_details', 'length', 'max'=>200),


		// The following rule is used by search().


		// Please remove those attributes that should not be searched.


		array('surveyQuestion_id, survey_id, surveyQuestion_content, surveyQuestion_details', 'safe', 'on'=>'search'),


	);


}

the pk in surveyinfo is :: survey_id.

is that it?

Sorry dear paste code of relations functions …

ohhh sorry sir I don’t know what you’re pointing… hmmm the whole content of surveyinfo and surveyquestions model? sorry again sir.

check ur models it will have relations function below the rules function…

paste relations code here

nothing is there sir… only the default.

in both ::

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(


	);


}

so what is the scenario in both models mean what is the possible relation

like surveyinfo may have many surveyquesions etc

hmmm the relation is (1 surveyquestion can only be in 1 survey)… (1 survey has many surveyquestions). 1 to Many.

first you have to study about relations

try this link

http://www.yiiframework.com/doc/guide/1.1/en/database.arr#declaring-relationship

in SurveyInfo.php , your relations function should be like this


public function relations(){    	

 return array( 

        	'surveyQuestions'=>array(self::HAS_MANY, 'SurveyInfo', 'surveyinfo_id'),

 		);

}

in SurveyQuestions.php , your relations function should be like this




public function relations(){

 		return array( 

        	'surveyInfo' => array(self::BELONGS_TO, 'SurveyQuestions', 'surveyinfo_id'), 

    	);	


 }

where surveyinfo_id is the foreign key in ur database surveyQuestions table

surveyQuestions is the relation name you will use, where you want to retrieve date in surveyInfo