Best practice in dealing with multiple models

Now I have most of my business logic sitting in CakePHP controllers.
I am thinking to rearrange my business logic, such that I can have Fat Model and Slim Controller. Everybody suggests the same. Here is my dilemma:

Controller A:
action aA()
{
fA(); //function of model A
fB(); //function of unassociated model B (needs a update)
}

Should I rearrange such a function as follows?

Controller A:
action aA()
{
fA(); //function of model A
}

Model A:
fA()
{
fB(); //function of unassociated model B (needs a update)
}

Here I access the other model B which is no way associated to A, from within model A. Is this a good practice?

So as part of an action, Controller, should talk to different models? or Controller should just handover the task to its model and model should talk to other models as he wishes?

Any other better approach?

Sincerely,
Srinivas Nayak

You can use $this->loadModel(‘ModelB’) and then you can use the methods of ModelB in the Controller A. No need to write the method related to ModelB in ModelA.

Your scenario sounds verid, so it may be a database design problem. Any way you can use the event system

I think that in MVC theory the situation you describe shouldn’t really be happening.

If you need to use the functionality of a certain model, then it should be associated with the controller / model that calls it somehow.

For instance, if you have a notices model that is connected to users model, but want to call a method from notices from inside of the articles controller (or model), which belongs to users but isn’t directly connected to notices, you could call it through users:

$this->Articles->Users->Notices->method();

Of course in reality the theory may not always apply in which case aavrug’s answe comes in handy :slight_smile:

But as for whether it’s better to call a model method from inside a controller or model: It really depends on the functionality of your app. Putting too much code into the models will make it inflexible, but keeping too much in the controller will make it fat. So it’s simply a matter of being smart about it and planning your app carefully.

Thanks ali, I too feel,

If you need to use the functionality of a certain model, then it should be associated with the controller / model that calls it somehow.

And this is actual dilemma.

But as for whether it’s better to call a model method from inside a
controller or model: It really depends on the functionality of your app.
Putting too much code into the models will make it inflexible, but
keeping too much in the controller will make it fat. So it’s simply a
matter of being smart about it and planning your app carefully.

As you said, in one hand we may make our models inflexible and on the other hand controller fatty. Here we may need to decide on the trade off.

Sincerely,
Srinivas Nayak