Search code examples
phpoopseparation-of-concerns

Where should I keep the queries? entity class or control class?


I am trying to make my project completely object oriented and I am separating the control, entity and boundary classes (you can call it m,v,c) On my former projects, I was creating functions in the entity class (model) and when the object is created, I would apply changes with a function inside that entity class.

post1=new post();
post1::save()

This worked fine, but I am unsure about the use in the real world, like, out of university. Is my way of doing it, (calling a commit changes function,(or save, you name it) OR should I create an instance of the object and send the object to a control class, like

controlclass::insertpost($post1)

which one is more practical? I did not have time to experience both, so I am willing to hear from people who have an personal preference on this. or someone can say "we use this way in our company."


Solution

  • In MVC way, any logic that deals with model data should be implemented by the model. e.g. the post itself should save changes applied to data it represents to the data storage, not controller or any other classes. Controller only serves as a glue (or bridge, or connector, whichever you prefer), between the data itself (model) and how it would be displayed (view).