Favorites widget help

I’m a very experienced developer, but new to Yii (and a little less new to PHP). I’ve read most of Agile-Web-Application-Development-with-Yii11-and-PHP5 and seemingly lots of parts of the official documentation and I’ve listened to 50, yes all 50, YiiRadiio pod casts, but do not seem to have the knowledge necessary to accomplish a database select and place the results in a widget in a sidebar.

I have about a dozen database tables all decked out with CRUD using Gii. I have a table called user favorites and its records point to records in other tables. I have tried piecing together the parts necessary to accomplish this, but I just get an empty sidebar. The sidebar is there, just empty. I can place the operations menu from the CRUD pages in the sidebar and it works fine, but I can’t seem to get my widget to do anything.

What I need is an end to end explanation of exactly what to do. I’ve read so many snippets of various kinds that I am now befuddled and don’t know what to do.

I am using the triplet layout from Yii Themes and have applied it in all my controllers. No problem.

I have created a function in the UserFavorite model:

public function findUserFavorites()

{


	$dataProvider = new CActiveDataProvider('UserFavorite', array(


		'criteria'=>array(


			'condition'=>'user_id=3',


			'order'=>'insert_datetime DESC',


		),


		'pagination'=>array(


			'pageSize'=>6,


		),


	));


	


	return $tdataProvider;





}

Inside of my protected folder I have created an extensions folder inside of which I have created a widgets folder inside of which I have placed a Favorites folder which contains a views folder.

In the Favorites folder I have a Favorites.php file which contains:

<?php

//Yii::import(‘zii.widgets.CPortlet’);

class Favorites extends CWidget

{

private &#036;title='User Favorites';


public &#036;displayLimit = 6;








public function getUserFavorites()


{


	return UserFavorite::model()-&gt;findUserFavorites();


}





	


protected function renderFavorites()


{


	&#036;this-&gt;render('favorites');


}

}

In the protected/extensions/widgets/Favorites/views folder I have a favorites.php file which contains:

<ul>

&lt;?php foreach(&#036;this-&gt;getUserFavorites() as &#036;favorite): ?&gt;


	&lt;div class=&quot;favorite&quot;&gt;


    	&lt;?php echo CHtml::link(CHtml::encode(&#036;favorite-&gt;title), array('topic/view', 'id'=&gt;&#036;favorite-&gt;item_id)); ?&gt;


	&lt;/div&gt;


&lt;?php endforeach; ?&gt;

</ul>

Here’s the attributeLabels function from the UserFavorite model so you can see the fields in the table:

public function attributeLabels()

{


	return array(


		'id' =&gt; 'ID',


		'user_id' =&gt; 'User',


		'title' =&gt; 'Title',


		'description' =&gt; 'Description',


		'what' =&gt; 'What',


		'where' =&gt; 'Where',


		'item_id' =&gt; 'Item',


		'behavior' =&gt; 'Behavior',


		'weight' =&gt; 'Weight',


		'status' =&gt; 'Status',


		'insert_datetime' =&gt; 'Insert Datetime',


		'last_edit_datetime' =&gt; 'Last Edit Datetime',


		'insert_datetime_utc' =&gt; 'Insert Datetime Utc',


		'last_edit_datetime_utc' =&gt; 'Last Edit Datetime Utc',


	);


}

The user_id=3 in the findUserFavorites function in the UserFavorite model will be replaced by the current user’s id.

The topic/view in the views file will be replaced by a value determined in a switch statement based on the what field of the user favorite record.

I am doing anything right?

All help is appreciated.

Thanks in advance,

Alex Adams

alex@a2technology.com

Hi… welcome to the Yii forum

As first thing I would suggest you to use the code directive when posting code so that it’s more readable (<> button on the editor toolbar)

regarding your widget… check the documentation about widgets - http://www.yiiframework.com/doc/guide/1.1/en/basics.view#widget

Spoiler: you need to override the init() and run() methods… as you don’t have them… no code is executed in your widget