Best practice app structure

I am still getting used to the overall structure of the app in Yii (and any MVC for that matter). I’m trying to understand the best practice structure of where to put the different models and views etc.

So I currently have a ‘fees’ model, basically has fees/sales type data. I’ve modified the view to display the fees grouped in one particular way, and I would like to create another view of this fees/sales data grouped another way.

So the question is, do I need a new model that queries the db based on this new field, or do I add another view to the same ‘fees’ model and just restructure the query in there, so that in the one fees view, I essentially have two separate views to display the data in different ways ?

More specifically, in the fees table there is a ‘sales’ field and a ‘settles’ field. I built the view based on the ‘settles’ field currently, and I want another view that displays the data by ‘sales’ field. So in the FeesController ‘actionView’ I query the table WHERE the ‘settles’ field is a certain date. I then pass that query result to the view.php to display. How do I structure it so that a similar actionView can return a resultset based on ‘sales’ instead of ‘settles’, but then display the data in a similar format to the way I’ve got the ‘settles’ data. Hope that makes sense

Thanks - any pointers to info on the best practice structuring these apps would be a help too.

I’d create two separate models in this case.

can u provide any more info or examples ? The thing that confuses me is that a model generally represents a table right?

If so, my requirements are not for a new table, just a different representation of the same table.

A table or a view, correct.

When you create a model with Gii it extends from \yii\db\ActiveRecord because it’s using data from a database. But look at the LoginForm.php or ContactForm.php. They both extend from yii\base\Model and are used to work with 2 views.

Hmm… was SQL view or PHP view meant? :)

A similar problem would result from a single ecommerce catalog table containing both product and inventory data.

In your situation, you could create a single Transaction model via Gii. Then based on the Transaction model, generate one controller and view set for Fees and a second controller and view set for Settles. Either set of views can be accessed independently using their separate controllers. If you wanted to statistically summarize Fees and Settles and provide a point of access for the two controllers, you could create a controller through Gii (along with a view) and call it something like Dashboard, then create Fees and Settles portlets (blocks) to display status information as well as to provide a couple of buttons for accessing the Fees and Settles controllers.

As a Module, your directory structure might be:


modules

    modulename

        controllers

            FeesController.php

            SettlesController.php

            DashboardController.php

       models

            Transactions.php

       views

            dashboard

            fees

            settles

       widgets

            fees

            settles

       Module.php

If you found that you needed separate models for some reason, you could create a second model by extending the Transactions model, then add new actions to overload the corresponding actions of the same name in Transactions or create entirely new actions.

Thank you very much for the detailed explanation, that is very helpful