Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC: Correct pattern to reference objects from a different model

I'm using CakePHP2.3 and my app has many associations between models. It's very common that a controller action will involve manipulating data from another model. So I start to write a method in the model class to keep the controllers skinny... But in these situations, I'm never sure which model the method should go in?

Here's an example. Say I have two models: Book and Author. Author hasMany Book. In the /books/add view I might want to show a drop-down list of popular authors for the user to select as associated with that book. So I need to write a method in one of the two models. Should I...

A. Write a method in the Author model class and call that method from inside the BooksController::add() action...

$this->Author->get_popular_authors()

B. Write a method in the Book model class that instantiates the other model and uses it's find functions... Ex:

//Inside Book::get_popular_authors()
$Author = new Author();
$populars = $Author->find('all', $options);
return $populars;

I think my question is the same as asking "what is the best practice for writing model methods that primarily deal with associations between another model?" How best to decide which model that method should belong to? Thanks in advance.

PS: I'm not interested in hearing whether you thinking CakePHP sucks or isn't "true" MVC. This question is about MVC design pattern, not framework(s).

like image 949
emersonthis Avatar asked Dec 06 '13 13:12

emersonthis


3 Answers

IMHO the function should be in the model that most closely matches the data you're trying to retrieve. Models are the "data layer".

So if you're fetching "popular authors", the function should be in the Author model, and so on.

Sometimes a function won't fit any model "cleanly", so you just pick one and continue. There are much more productive design decisions to concern yourself with. :)


BTW, in Cake, related models can be accessed without fetching "other" the model object. So if Book is related to Author:

//BooksController
$this->Book->Author->get_popular_authors();

//Book Model
$this->Author->get_popular_authors();

ref: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html#relationship-types

like image 54
Costa Avatar answered Nov 14 '22 18:11

Costa


Follow the coding standards: get_popular_authors() this should be camel cased getPopularAuthors().

My guess is further that you want to display a list of popular authors. I would implement this using an element and cache that element and fetching the data in that element using requestAction() to fetch the data from the Authors controller (the action calls the model method).

This way the code is in the "right" place, your element is cached (performance bonus) and reuseable within any place.

That brings me back to

"what is the best practice for writing model methods that primarily deal with associations between another model?"

In theory you can stuff your code into any model and call it through the assocs. I would say common sense applies here: Your method should be implement in the model/controller it matches the most. Is it user related? User model/controller. Is it a book that belongs to an user? Book model/controller.

I would always try to keep the coupling low and put the code into a specific domain. See also separation of concerns.

like image 21
floriank Avatar answered Nov 14 '22 17:11

floriank


I think the key point to answer your question is defined by your specifications: "... popular authors for the user to select as associated with that book.".

That, in addition to the fact that you fetch all the authors, makes me ask: What is the criteria that you will use to determine which authors are popular?

I doubt it, but if that depends on the current book being added, or some previous fields the user entered, there's some sense in adopting solution B and write the logic inside the Book model.

More likely solution A is the correct one because your case needs the code to find popular authors only in the add action of the Book controller. It is a "feature" of the add action only and so it should be coded inside the Author model to retrieve the list and called by the add action when preparing the "empty" form to pass the list to the view.

Furthermore, it would make sense to write some similar code inside the Book model if you wanted, e.g., to display all the other books from the same author.

In this case you seem to want popular authors (those with more books ?), so this clearly is an "extra feature" of the Author model (That you could even code as a custom find method).

In any case, as stated by others as well, there's no need to re-load the Author model as it is automatically loaded via its association with Books.

like image 1
savedario Avatar answered Nov 14 '22 16:11

savedario