Planning application structure with Yii

Hi All,

I am new in Yii and MVC, and I am trying to understand how an overall applications is built.

Let me give you an example. Suppose I want to build a store. This store should have the following features:

Store Front

-Product list

-Product view


User profile

  • Settings

  • Order History

Admin backend

  • Edit products

  • Edit categories

  • Handle Orders

Now, how would you build such application with Yii? Would you create a GRUD for each one of those functions?

Or would you create just 3 controllers:




And a model for each "functionality":






How would you use the OrderModel inside the AdminController? Would you use relations?

Thanks all… This is indeed a different way of thinking.

You should start with models first, and create all models that you might need for you application. Then, based on model, you can create CRUD actions for each model(note that not all models need to have their own controller).

For admin, you can create new module and to keep all admin related controllers in this module. And yes, you can share same model between few controllers or modules.

Thank you. I think I am starting to understand…

I noticed in the Blog tutorial example here that when a post is created they have the tags created within the post model, like so:

$this->dbConnection->createCommand("INSERT INTO PostTag (postId, tagId) VALUES ({$this->id},{$tag->id})")->execute();

However, this means that only the post model can create tags. Wouldn’t it be smarter to implement all tag related operations in the tag model?

If I would have created this application without a framework, I’d have a class for tags that would insert a new tag. To do the same with Yii, would I have to create a new function in the tag model and call the function from the post model? Is that the “proper” way to do this?

@Bymannan: The code you listed:

$this->dbConnection->createCommand("INSERT INTO PostTag (postId, tagId) VALUES ({$this->id},{$tag->id})")->execute();

isn’t creating new tags, it’s actually inserting records into a cross reference or intersect table – PostTag – which resolves the Many to Many relationship between Post and Tag.

Yes, sorry,

So this creates a new tag which is the proper way of doing it:

$tag=new Tag(array('name'=>$name));


However, I have two questions:

  1. Why the PostToTag table is not updated using the PostToTag model object?

  2. I thought the relations system should take care of those things. If two tables are related shouldn’t they automatically updated?