1

I'm just wondering, I'm not an expert on MVC but have been shifting our code towards a better MVC structure on the Zend Framework over the past few years. I basically have different models handling different sets of logic for different entities in the database. For example, a Product model, and a Customer model, etc.

Is it OK for my Product model to instatiate a Customer model, so that it can use some of the Customer functions? OR is this the job of a controller, to call the relevant functions of Customer, and pass the results to the Product? Let's say the Product needs to know if a Customer has certain records within it, so that the Product can decide on the correct data to return to the controller.

I am just curious on the 'best way'. I would want to instatiate a "model within a model" since the logic is needed from different controllers which call the same Product model function.

Many thanks!

1
  • 3
    Controllers are supposed to be extremely lightweight, they're just there to hand data to the model (which in reality should be a collection of interacting classes) and collect the results. The models should know how your application works, the controllers don't need to know Commented Jul 19, 2012 at 7:48

2 Answers 2

0

IMHO it is OK to instantiate a "model within a model" as shown in case of CakePHP here:

CakePHP: calling other Model functions

You may also check these links One, Two and Three

Sign up to request clarification or add additional context in comments.

Comments

-2

You use bellow code

$product  = new Application_Model_Product();  // create model class object, any model name which you want to user 
$product->getProductList();  // just any function from model

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.