Need help with a new Application's structure

Hi everyone!

I’m very new to Yii but I think it’s a really great framework. I followed most of the tutorials and now I want to start my own application and I need your help.

The structure of my website will be:

Category1 (type=articles)

-Sub-category 1-1


—Article 1

—Article 2

—Article 3

-Sub-category 1-2


—Article 1

Category2 (type=news&events)

-Sub-category 1

–Sub-sub-category 1




-Sub-category 2

What I want to achieve:

  1. On the category view to retrieve all sub-categories and recursively sub-sub-categories

  2. On the sub-sub-categories to list all the articles/ news with links to a full article/ news

Should I create a different controller for each category type, or have a "type" field in the category table?




-type (ex: articles, news)


I’d really appreciate your help!


If I’m not mistaken, for this purpose definitely there should be one controller (for controlling all menu related operations) and whole menu structure (no matter how complicated) should be generated dynamically.

I would advice you to start from Yii Cookbook article titled "Creating a database-driven hierarchical Structure combined with CMenu and superfish", where this problem is quite nicely and clearly explained.

Hi there,

There a few ways to do it but I won’t mix articles and news on the same table even if they are more or less the same. Just for the sake of portability afterwards. Create a Articles and a News (model/controller/view) and let them have their own table.

Then have another one Category which I would:

id <------------.

name |

parent_id <–.

After you can easily create a recursive function that loops through the categories. That will help you create a menu as well explained by Trejder. Check that tuto… is a good reference (Also, do some search on the forum and Wiki:

In my opinion it depends 100% on project construction/plan/sketch. In some projects news are no different to articles - they might be a little bit smaller but looking from DB side, they use exactly the same fields and structure. Did some project like this. In this situation using two different tables/models/controllers/views is just a waste of time of developer. But you may disagree with me.

I do agree with you 100%, but I’ve found my self making some SQL exports and deletes due that afterwards the client wishes to do something else somewhere else.

If it was a project of mine, then yes, it is perfectly understandable the laziness of a programmer :) and by having a field that states this is a news or an article is enough.

In both cases, I think is correct.

Thank you guys for your reply!

Articles and News will be different, that’s why I want to have separate tables. E.g Articles will “tags” field etc…

From the links you both sent me, I can see how to retrieve the the full tree and have it displayed as a menu. But for me I want to display in the Content every Category and a list of Sub-categories


Category 1 (Articles)

-Sub-category1 (link to Sub-category1 page)

>>Description of Sub-category 1

-Sub-category2 (link to Sub-category2 page)

>>Description of Sub-category 2

Sub-category 1 (Articles)

-Sub-sub-category1 (link to Sub-sub-category1 page)

>>Description of Sub-category 1

-Sub-sub-category2 (link to Sub-sub-category2 page)

>>Description of Sub-category 2

Sub-sub-category 1 (Articles)

-Article1 (link to Article1 page)

>>Teaser of Article1

-Article2 (link to Article2 page)

>>Teaser of Article2

I still don’t know very well how the Model/ Controller works in Yii.

A pseudo code of what I want to do is:

<if "category($type=articles/ news)" has children-categories>

–list Children-categories



–list Articles/ News


Where can I put this?


In the article I gave you link to you can find following part of text:

In the first line of code you have Hierarchy::model()->findByPk(1);. It renders menu structure beginning from DB entry with primary key = 1. If you analyse eariler in this text a part where data is being inserted to DB, you will see that PK=1 has a root position. All you have to do, is to change this line of code by giving different PK as argument.

If you would like to draw your menu starting from (in citied example) Third Entry, then you would do this by using code have Hierarchy::model()->findByPk(4), because Third Entry has primary key set to 4 (insert into Hierarchy (id, sort, parent, title) values(4, 0, 1, ‘Third Entry’) ). This way you can render menu starting from particular position in structure and all its subitems.