Should sub-objects be fetched in the Model or the

2019-09-03 10:49发布

问题:

This is a follow-up to my previous question: Should dependent rows be fetched in the Model or the Controller?.

Let's say that a Patron can check out Books. If, given a Patron object, where and how should the Books they have checked out be fetched?

Here are some possibilities I've considered:

  • In the Patron Model, add a getBooks method
  • When a Patron is instantiated, automatically get its books and store it as the books property
  • Create a method in the PatronMapper called getBooks that takes a Patron ID

What would you recommend? Thank you,

回答1:

I suggest you to go with:

When a Patron is instantiated, automatically get its books and store it as the books property

This way you don't have to manually worry about it