get or set model data in layout file

Hi,

What is the best way to get model data in particular layout file.

I want to set( in layout ) company name, copyright name … … from database value.

Well Kiran,

The best way to do is creating a CWidget and Call it in a layout or any of the file.So, i will recommend you to use CWidget :)

yes,

I had follow this link but not working and can’t get idea why to rander view file.

http://stackoverflow.com/questions/8438660/yii-how-to-retrieve-model-data-into-a-layout-page

is there any yii forum or yii wiki link to provide sample code?

Hey Kiran,

Could you please explain a bit more what you are trying to do exactly.So, that i can better answer your problem.And please Also Share your code here. :)

just want data from database ( using model ) in my layout file, so i can show the company name, copyright company name dynamically which is set in my db table…

Well you have also replied on Thid thread Where andy_s replied about this code.





Code in Widget Class :- 


class Userinfoblock extends CWidget {


	public function run() {


		$model=User::model()->findByPK(Yii::app()->user->getId());

		$this->render('userinfoblock',array('user'=>$model));

	}


}




And i have a code in My widget view file something like this :-




 <div class="usr_prfl">

    	<div class="usr_prfl_left">

    	<?php

    		if(!empty($user->userpic)){

    			echo CHtml::image($bUrl.'/uploads/user_avatar/thumb/'.$user->userpic,"",array("width"=>"60","height"=>"60"));

    		}else{

    			echo CHtml::image($tUrl.'/images/no_img.jpg',"",array("width"=>"60","height"=>"60"));

    		}

    	//echo CHtml::image($tUrl."/images/active_img_4.jpg","",array("width"=>"60","height"=>"60"));

    	?>

    	</div>

        <div class="usr_prfl_right"><h3><?php echo $user->first_name;?>&nbsp;<?php echo $user->last_name;?></h3><?php echo $user->address;?></div>

    </div>



And its work like a charm for me.In this Widget i am showing User’s information dynamically. Please check your code again and Check that you are getting result from Model->Find () Query or not.

Let it try .

Ahhh,

Have you tried my code. How i am displaying dynamic data in Widget. :o

yes i am sure you are rite,

because…

as i sent you stack overflow link there was an error in that link,

please see , http://stackoverflow.com/questions/8438660/yii-how-to-retrieve-model-data-into-a-layout-page

in that code author illustrate


 $models = Category::model->findAll();

which have to like,


 $models = Category::model()->findAll();

and that was the problem… and as i used that code so that i can’t get model data.

and

+1 for your fast reply :)

Ahhhh Glad to know that you have done it :lol:

The Awesome thing is … I got data in layout file directly,

$siteData = SiteSetting::model()->findAll(); // get model data

$arr = array();

foreach($siteData as $t)

{

&#036;arr[&#036;t-&gt;value] = &#036;t-&gt;value;

}

print_r($arr); // array of data

:)

Calling model queries on view file is not a good MVC practice.

Well,that is good if you want to go with that.

@ Kiran Sharma. IMHO. when you do things like this, you will end up in a serious mess. sometimes it look like a waste of time when you follow the MVC pattern but it’s not(code maintenance is one aspect that MVC greatly solve out of the box)

To use model directly in view is just for temperory purpose that to check things work or not,

I had use Widget for get model data in file.

my code is as follows,


<?php

class SitesettingWidget extends CWidget {


    public function run() {

        $siteData = SiteSetting::model()->findAll(); 

        

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

            'siteData'=>$siteData,   

        ));

    }

}

No worries for that … :)

Yuppp That is the correct way ;)

Cheers !! :)

In other and better way,

Solved Here