P
P
Prodion2019-12-27 07:56:18
MySQL
Prodion, 2019-12-27 07:56:18

Is there a problem in choosing a relation or is the database structure not correct?

During the week, from day to day, I rewrite everything anew. Stuck in one place due to the fact that it is not possible to design the database correctly. It turns out that everything seems to be correct, but when it comes to completion, I run into the wrong structure.
There are 3 tables:
Manufacturer.
- Has idand name.
Collection.
— Has id, manufacturer_idand name.
Colour.
— Has id, collection_idand name.
Logic:
One manufacturer can have many collections. One collection can belong to one manufacturer. A collection can have many colors. One color can only belong to one collection.
I must say right away that color is not a color in the usual sense, but a physical thing. If it were a color in the usual sense, then it is logical that one color can belong to different collections, but this is not the case. In our case, color is what we use as a name for an unnamed item. Collections are not a collection in the Laravel sense, but simply the name of a table and a model.

— 1. Производитель.
— — 1. Коллекция.
— — — 1. Цвет.
— — 2. Коллекции.
— — — 2. Цвет.
— 2. Производитель.
— — 3. Коллекция.
— — — 3. Цвет.
— — 4. Коллекция.
— — — 4. Цвет.

It seemed logical to use a relationship hasMany()(one-to-many), but it turned out to be a little more complicated.
Pages:
Colors and collections unknown to us. We see only their total number.
(Страница производителя)
Производитель — Коллекции — Цвета
Имя_____________2(две)______2(два)

(Страница коллекции)
Производитель — Коллекции — Цвета
Имя_____________Имя_________1(один)
Имя_____________Имя_________1(один)

When we opened the color page.
(Страница цвета)
Производитель — Коллекции — Цвета
Имя_____________Имя_________Имя
Имя_____________Имя_________Имя

On the manufacturer's page, everything is very simple and clear. We can link the manufacturer model to the collection model via hasMany(), and to the flower model via hasManyThrough(). Then we can calculate the total number of collections and colors of this manufacturer using withCount().
On the collection page, too, everything is very simple. We have feedback with the manufacturer's model belongsTo()and communication with the color model via hasMany(). We use withCount()to count the total number of colors belonging to this collection.
The flower page is a dead end. We can get the collection to which the color belongs because we have feedback from collections via belongsTo(), but we don't have feedback from the producers model. Laravel has no feedback for hasManyThrough().
It turns out that either my vision of solving this problem is obviously not correct, or there is a problem with the database structure. This is my first development experience, so I would like to do the right thing, not just do the how. How would you solve this problem? It is desirable to have the most correct solution so as not to teach the beginner bad. However, I will be glad to any idea and suggestions.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
vism, 2019-12-27
@PRodion

The question is so well asked that it makes me want to answer it!
Your structure is correct, as far as I understood from the description.
And sometimes such "link chains" are not from 3 links, but from 10 :)
It's just that Laravel has hasManyThrough through 1 table, but this does not oblige you to anything. I recently saw the hasManyThrough package for 2+ intermediate tables.
To get feedback on belongTo, just use this, no reverse hasManyThrough is needed here:

$color->collection;
$color->collection->manufacturer;

Y
Yuri Kulaxyz, 2019-12-27
@Kulaxyz

If I understand you correctly, a color has only one collection, and collections have only one producer. Then in the model you can simply make a method

return $this->collection()->first()->manufacturer();

I
Ivan Melnikov, 2019-12-27
@immelnikoff

The most important thing in database design is to thoroughly understand the subject area.
Strange condition. Can't two different collections be blue?
So an item can have multiple names?
Why not make a separate Collection table and a separate Color table and link them with an M:N table?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question