views:

133

answers:

6

My problem is in somewhere between model and controller.Everything works perfect for me when I use MVC just for crud (create, read, update, delete).I have separate models for each database table .I access these models from controller , to crud them . For example , in contacts application,I have actions (create, read, update, delete) in controller(contact) to use model's (contact) methods (create, read, update, delete).

The problem starts when I try to do something more complicated. There are some complex processes which I do not know where should I put them.

  1. For example , in registering user process. I can not just finish this process in user model because , I have to use other models too (sending mails , creating other records for user via other models) and do lots of complex validations via other models.
  2. For example , in some complex searching processes , I have to access lots of models (articles, videos, images etc.)
  3. Or, sometimes , I have to use apis to decide what I will do next or which database model I will use to record data

So where is the place to do this complicated processes. I do not want to do them in controllers , Because sometimes I should use these processes in other controllers too. And I do not want to put these process in models because , I use models as database access layers .May be I am wrong,I want to know . Thank you for your answer .

+1  A: 

Just a short comment (no solution) AFAIK that is an eternal question - MVC is just a pattern, and as such, is in theory implementable cleanly. In practise, due to limitations set by available tools (such as programming language library contents and UI component interface design..) you have to make local decisions. The important thing is that you aim to separate these...and not have everything in one mess. I take my comment off the air and am left to see if someone has a "final solution".

Hubbard
I agree mostly with this...
alex
+1  A: 

In MVC, you should place those things in the model (for reuse reasons for one).

However, in HVMC, you could place them wherever (such as in a controller) and call the controllers from within your application.

alex
What about using models within another ? Can I call any model from another model ?Thank you
Oguz
+1 for Oguz and @ alex same question for controller?
OM The Eternity
Yes you can, I often do within another model `if ($this->userModel->loggedIn()) { }`
alex
Controllers can call other controllers through the HVMC pattern. You can even have controllers only to be called within your application. Generally you check something like `if ($this->request === Request::instance())`
alex
Using superglobals like $_SESSION from a Model seems smelly to me. I'd keep any database state changes related to users registering/logging in within the model, but all the session management should be kept in the controller (or more likely a helper class called from within the controller)
Lotus Notes
@Byron I've still not sure if I'm doing it the best way, but it has worked relatively well. To clear things up too, I usually use the session *library* from Kohana, which is a neat wrapper that adds a few things here and there.
alex
+1  A: 

I would make your controllers simple.

In many ways the model allows you to offload a lot of the complexity that would otherwise occlude your controller code. Its this division of complexity which will make your code more easily understood, and easier to maintain.

personally I try to keep my models resembling real world objects, not databases tables or rows. It makes it much easier if you have made things speak in more readable terms. A single real world object might involve 5 or 6 database tables... And it would be a rather large hassle to speak with 5 or 6 models, when all you want to do is turn on a switch, or pick a flower, or paint an icon, or send a message.

Bingy
@Bingy Can U pls refer any suh tutorial which can guide me in the manner u resemble ur MVC? Even I like to resemble things to real world...pls suggest
OM The Eternity
not off the top of my head...its something that will make sense with practice. Once you do it once or twice, you will have a better idea. Don't be afraid to have a go. When you come back to it a few days, or a week later you will be full of ideas on how you could have done it better.What you want to avoid is painting yourself into a corner. eg having session data spread across the controller/model/ and other handlers. Since you have created many models for each of your tables, prehaps you would benefit from having a "super Model" that references your many smaller models.
Bingy
+1  A: 

For simple tasks I would write action helpers (e.g. sendNewsletter).

For sophistocated tasks I woud create services (eg. email, auth etc.).

takeshin
Thank you for your answer, I think that your answer is like what I think ,I mean this is the way I think too , Can you give any sources to guide me about this way of writing code , or creating mvc structure .
Oguz
If you prefer book, read `Zend Framework 1.8 web application development`. For online resources, go google for 'zend framework action helpers' and check http://www.slideshare.net/weierophinney/playdoh-modelling-your-objects-1766001 (The famous phrase: Applications are like onions ;)
takeshin
A: 

What's wrong with a controller using multiple models? Isn't the point of MVC to make the model reusable? In your first scenario, it's perfectly fine to send emails and manipulate other model objects from wherever the "register user" controller code is.

In regard to your second scenario, why can't SearchController use ArticleModel, ImageModel and VideoModel? It's fine to have a controller without a model. SearchController doesn't need a SearchModel class, it just uses the other model classes.

I'm trying not to get into a rant about MVC in web apps, but basically, IMHO the controller is just a high-level list of steps to complete an operation. As a rough example, the "register user" controller code should do each of the following steps in roughly one or two lines of code:

  1. Validate the input
    1. If not valid, redisplay the form with an error
  2. Create the new UserModel object from the form input
  3. Insert the new UserModel object into the database
  4. Create/edit whatever other model objects are necessary
  5. Send off an email to the new user
  6. Display a "registration successful" page

How those steps are coded largely depends on whatever framework/architecture you're using.

Tom Dalling
There are lots of reasons of Why I can not use Controller to Validate data , or to do other complex processes. For example, sometimes I have to repeat this methods from another controller so I should not re write this code in another controller. Also Controller does not know the validation rules model required, It just supplies data , and model validates this data .I am not the best person to understand the reasons why you do not have to validate data in controller , but I am sure that you must not do that in controller .
Oguz
It doesn't matter if the model class contains the validation function. The controller just needs a true/false indicating whether the input is valid or not.
Tom Dalling
A: 

Keep your controllers clean. For backend processing use Manager classes like MailManager etc..

yanis