How To List Status Messages On Front Page In Yii?

I am working on a project where my client wants to show recent status messages from users on home page, including photo, time, country, city, etc ( I mean more than 2 tables joins). I am stuck here because I am to Yii and I don’t have any idea. How should I implement this system.

I have couples of question regarding this situation. (Please don’t bother I am fairly new)

  • Which controller action should I use? SITE/INDEX or should I work on new one?

  • How should I display those headlines.? Should I use CListView() / CGridView or something else?

  • Should I write custom SQL using DAO?

  • What best practices would you suggest for me in this situation?

Thanks in advance.

Hi Ejaz

I would not use site/index. I would use my own action, but it depends on what you want to do.

No need to use DAO. AR is better; it can do more.

Can you provide a basic screenshot/wireframe of what you’re trying to accomplish? Also, how many/what tables are needed?

Thanks,

Matt

Thanks for reply Matt. It is just like recent updates/statuses of friends in facebook. I have user inputted status message with attachments like photo, links, status message etc.

1st Table (users):

  • user_id

  • first_name

  • last_name

  • email

  • password

  • joined_on

  • profile_photo (relation with photos table)

  • country (relation with country look up table)


2nd Table (status message)

  • id

  • status

  • attachtment_id


3rd Table (attachments)

  • id

  • attachment_link

  • user_id


4th table (photos)

  • id

  • photo_id

  • photo_name


5th table (Country)

Basically there are 4-5 tables related with each other. I am not so good in explaining in English. I hope you can understand my goal. It is just like recent updates in facebook, recent tweets in twitter etc.

As others have said there are a number of ways to go about this

Unlike Gerhard says, The ‘use on home page’ requirement dictates using ‘site/index’ some how. Although what is meant by “Users home page?” Then maybe user/index action.

This is a matter of choice: for just listing some information from a db_table, SQL/DAO is usually faster.

You could display the info in the ‘site/index.php’ file, use a renderPartal(’_status’,…), go with a widget (see Blog Demo - Tag Cloud, Recent Comments)

For the index file or renderPartal() options above, you will need to add the $model/$dataprovider as needed by your choice of display widget. The renderPartal() and widget() options allow reuse, if needed elsewhere on the site.

Most tutorials dictate that accessing a db should be done in the controller or in a widget. However you could do it in a partal view (many would say this is a bad idea.)

So my initial thought would be to use a ListView and not a GridView because of the 1 - N relationships.

[size="2"]To make things simple[/size]

[list=1][]Make sure all relations are correctly set up in models[]build a CActiveDataProvider in SiteController’s actionIndex method and pass it to site/index view[list=1][]make sure to use eager loading when retrieving your relations[/list][]In site/index view, construct a CLIstView and pass it the data provider[list=1][*]try to break this step into 2 parts by using renderPartial to display the CListView. Ie. create a new file called _user_status_summary.php and add the CListView to this. Then, in site/index view, renderPartial the newly created view. This helps to break complex views into more manageable pieces. It also makes it easy to [size=2]rip out all the logic from the controller and view and stuff it into a widget if you want to reuse this in other parts of your site. [/size][/list][/list]Depending on your tables’ sizes and the fact that these queries are being executed from the homepage, you’ll also probably want to cache these results using a query cache. This should be done after the fact once you’ve got your views outputting the correct data.

Post back if you get stuck along the way.

Matt

Thanks Matt. Your solution seems perfect to me. I will go with it. Thanks again. :)