In a MVC structure, can we create a model that represents a View (SQL)?

Asked

Viewed 263 times

5

I was arguing with a friend of mine about using views (I mean Mysql, not Mysql Pattern MVC).

Hence a doubt arose in the following sense: A Model is used to represent a data structure, and you can read and write data in a table through it. So I could use, in an MVC structure, a model representing a view SQL (so it usually represents a table)?

For example, if I have a view in Mysql, I can represent the same through a model in a framework, like Cakephp, or Laravel, or Django?

I say that because the models generally have methods for writing data. And views are only ways to view data.

If I made a model representing an SQL view, could be violating the principle of model?

2 answers

7


In theory, it can. Views in Mysql can insert and update records. Only cannot delete records from the tables involved.

Of course the mounting of the MVC pattern on top of a View in SQL needs to be more judicious. Some operations will have limitations and you will have to work on them.

The ideal is still to build Models on top of tables, with the application having full control over the database.

  • 1

    You can enter, update, but you can’t delete. That’s amazing! You’ll know how this data is handled internally! About the fact that the application has domain, you mean in the sense of: If I already have the model to work with the data, I don’t need an sql view?

  • In theory it does not need because the application is in charge of abstracting the data for you. What I consider valid for use of Views is the concept of Views Materializadas, consolidating the projection of a View on top of physical data. That is, the View comes into being physically.

6

You can put whatever you want a model. It represents data organized in such a way that it can be used by controller to produce views.

It is common for the models to represent tables in a database. In this case, it is common for the model to read and write in the database. But this is not a requirement.

One view of SQL represents existing data in the database, simulating what would exist in a physical table. It functions as a logical table.

So nothing prevents it from being used as a basis for the model, as long as the logic for accessing the view is appropriate.

Of course, you can only change data through view if the database has this capability, which is not common in some systems. And even where possible, there are limitations.

Otherwise there may be some limitation imposed by frameworks quoted.

I made a reply who talks more about this because it is something confusing and at bottom is opinion, even the same person has different opinion depending on the moment.

  • 1

    At least in the Aravel, I would create a kind of "lock" where no data could be inserted. That would make it understandable to everyone that this is a sql view

Browser other questions tagged

You are not signed in. Login or sign up in order to post.