mardi 13 août 2019

Where to define condition of the query in mvc pattern (PHP)

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...?

Aucun commentaire:

Enregistrer un commentaire