1

I have a design question. I'm thinking about that for a long time and can't decide how to do it correctly (and maybe a repository on top is a better choice..)

I built an own mvc pattern and I'm asking me myself, where I should set the conditions for my queries? Let's say:

UserModel has the function getByUsername($username). In this case I would set the WHERE clause of my query in the function of my model.

Another way could be getByUsername($username, $where = []). With this I can choose where to set the WHERE clause. I could set it in the UserController and pass it to the function.

This is my question: What is the best practice in this situation? The frustrating thing is, that I'm using the function with different WHERE clauses. Should I make different functions in the model like "getByUsernameWhereABC($username)" and so on or should I stay with the solution to put these information in the Controller?

This not only affects the WHERE clause. Also the columns are a problem. I don't need the same columns everytime I'm calling the getByUsername function. So I can set these parameters in Controller and pass it to the function.

But I don't think that this is the correct way. My Controller seems to be half a model...?

arkert
  • 52
  • 10
  • It definitely belongs in a model, but the question is whether you are using dmm or more of an active record approach. How much do you want to abstract. It also depends how you define mvc ;) I would suggest you have models that know how to access the database, and models that ask for information from those models but which have no idea how those models get the data. – Tim Morton Aug 14 '19 at 00:58
  • I want a good design that fulfill flexibility and maintability. My first project was a mess after a while. I don't want to repeat this situation. Because of this I tried a mvc approach, which I like. In detail there are many questions like this. Your opinion is, that I should implement a variety of functions in my model like "getByUsernameFor[SpecificPage]()" or "getByUsernameWhere[ABC]()? Another example with comments: "getByNews()" and "getByProfile()" etc.. But I'm still confused about columns. How I easily tell that without to make a mess in Controller? – arkert Aug 14 '19 at 06:27
  • This might help: https://stackoverflow.com/a/5864000/727208 – tereško Aug 14 '19 at 08:44
  • ^^^ Excellent resource. I'm going back to reread... – Tim Morton Aug 14 '19 at 13:32

0 Answers0