Jump to content

Why am I duplicating methods between models?


Recommended Posts

Should I be able to reference any model from any other model? Currently only the controller can access any model it wants information from but once you get into the model, you can't communicate with other models.


The situation I have right now is a settings page I have where it lists all the items from a table in the database and allows you to modify them or add new ones. So I have a settings model that retrieves all items from that table and the view displays them on the page. I have another model for another page that actually uses these table items and again I need to get all the items from that table in this model. Do I have the same function in both models? Do I move them to a common model that's shared? Do I just choose one and reference them from there?

Link to comment
Share on other sites

It seems to me that the models shouldn't communicate between each other, but you should use the controller to perform that logic.


That being said, having individual models with presumably only one public method each such as one for creating a record, one for reading a record, one for updating a record, one for deleting a record (what's it spell?) is way to specific.  I would say you should have one settings model which does all these tasks.  You could and should also extend that model from some likely abstract model which can do tasks common to all models.

Link to comment
Share on other sites

Laravel's eloquent models and others I can't remember the name of tend to have a method for every other model that has a defined relationship to another model.


SalesRep models know they belong to an Office model.


Office models know they own SalesRep models.


SalesRep models know they own CustomerRelationsMessage models.


CustomerRelationsMessage models know they belong to both a SalesRep model and a Customer model.


Customer models know they own CustomerRelationsMessage models and OrderInvoice models and PaymentHistory models.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.