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

5

I was arguing with a friend of mine about using views (say MySQL, not the pattern MVC).

A question arose in the following way: A Model serves to represent a data structure, being able to read and write data in a table through it. So, could I use a model in an MVC structure that represents 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 this because models usually have methods for writing data. And views are just ways to visualize data.

If I did a model representing a view , could it be violating the principle of model     

asked by anonymous 14.09.2015 / 19:12

2 answers

7
In theory, it can. Views in MySQL can insert and update records . They can not only exclude records from the tables involved.

Of course, mounting 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 them out.

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

    
14.09.2015 / 19:22
6

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

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

A view of SQL represents data in the database, simulating the which would exist on a physical table. It works like a logical table.

Then nothing prevents it from being used as the basis for the model as long as the logic for view is appropriate.

Obviously, you can only change data through view if database has this capability , which is not common on some systems. And even where it is possible, there are limitations.

Otherwise there may be some limitation imposed by the frameworks cited.

    
14.09.2015 / 19:23