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!

rishijd
  • 1,294
  • 4
  • 15
  • 32
  • 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 – GordonM Jul 19 '12 at 07:48

2 Answers2

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

Community
  • 1
  • 1
000
  • 3,976
  • 4
  • 25
  • 39
-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
Hiren Soni
  • 574
  • 3
  • 11