Hey. I ask the question because I seem to lack a basic understanding of how everything works.

Situation: I am developing the first project using the MVC framework and MySQL database.

What is the data: Companies and News .

Each Company will have several photos. Therefore I make 3 tables in the database:

  1. Companies
  2. News
  3. Photos

In the Photos table I make the fields:

  1. id
  2. foreign_id (for one-to-many communication with Компаниями )
  3. filename (for file name)

Accordingly, from the code side, I have 3 models and 3 controllers. Responsible for working with photos PhotosController

Problem: now I want the News to have photos (of course, not the same as Companies :)). And here I come to the fact that I do not understand.

Question: what should I do in this situation - add an additional table for news pictures, similar to the Photos table and let's say it is Media . And link it to the news in the same way as Photos are related to Companies ?

Or expand the Photos table by entering the additional 'type' field there and designate to it who owns a specific entry - Company or News .

What are my doubts: if I make an additional table, I will have to do a separate controller which will duplicate the code of the PhotosController controller

If you make an additional field in the Photos table, then there will also be complications of requests for data sampling and anything like that, not ok.

What do they do in such situations so that the code is DRY and the base is Feng Shui? Please tell me more! Thank.

  • Why don't you make a controller 1) Companies 2) News; each controller works with its own photo, 4 tables. Why for such tasks to produce controllers and models. And the frame, as I understand it, Yii. - ukroficer
  • It is possible and three controllers and 3 tables (photo with the type) but somehow it is not convenient with usability. - ukroficer
  • Framework cake A model is created for each table there. So 4 tables - 4 models. If you do 2 controllers, they will be very fat in my opinion. There also have to cram and all that is associated with CRUD conventional data and + more work with CRUD photos. - Phoenix_n

1 answer 1

The question is why produce so many tables?

What is the data: Companies and News.

There are 2 controllers

  • company
  • news

All on this silence.

There are models 3 company, news, photo. 2 tables.

 company id|name|photo news id|title|full|short|photo 

photo - this is a field in which you can write an array of uploaded photos to the company or news, no matter how much they need! But for the news it’s enough for the announcement that one + is enough in the text of the news itself, there may be pictures, but this is already an editor’s job in the admin panel as it loads the pictures.

Model photo takes pictures for loading and processing everything.

why 4re tables then? Why keep it is not clear that it is not clear where + a place to occupy 2-3 times more.

And if you make a gallery then for each gallery you make one more table?

  • 3
    This is not very well stored in the photo array of photos. If you need a description for each photo or some other info? Therefore, for photos, a separate table is needed, just to understand what belongs to it, you need to make another column, for example type, you can even type enum, and store the values ​​of either news or company. IMHO of course - Barton
  • I can not fully agree with the answer. In news, we can only add a field under the news icon, store the name of the icon file there. One. I also need to attach to the news a lot of pictures. 10, and even 15. And with firms the same. - Phoenix_n
  • one
    The question was not that each photo needs a description and other Labuda. Yes, and in this array can be added. Just different tasks and each is solved in its own way. I gave everything for an example, not necessarily an array, you can 1 photo. And you need to attach pictures in the text or how? If in the text, then why a separate table for pictures? IMHO nonsense, what should be on the page in the text should be added to the editor. From what I saw everything can be done with 2 tables. The database is not large, as I understand it will not be a huge visit. pull once the data and that's it. What is easier to make select or select with join? - Artem
  • I did not say that it will be on the page in the text. I talked about communication. One company has many photos, one news has many photos. Photos are unique and not repeated. - Phoenix_n
  • Even if the list of links, you can do with 2 tables. - Artem