L
L
LRST2019-11-16 02:03:30
Laravel
LRST, 2019-11-16 02:03:30

How to implement and store inherited objects/models?

I am currently reading literature on design patterns. And an example is considered:
There is a Product class, BookProduct and CdProduct are inherited from it and they differ in some methods and properties.
BookProduct has numPages properties and a getNumberOfPages method, while CdProduct has playLenght and getPlayLenght.
And I can’t figure out how to implement this within the framework of Eloquent. Is this Product model supposed to be or is there still 2 BookProduct and CdProduct models, so then these are 2 tables?
And if there are many types of products, is it necessary to create a table for each type? On the other hand, if there is one table, then there will be too many columns. And if inheritance from BookProduct and CdProduct starts further?
In general, on paper, everything looks good and beautiful, but as I want to dig deeper into Laravel in practice, it’s a stupor.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Antonio Solo, 2019-11-19
@solotony

I do this
in the beginning, I inherit the MyProduct class from the base Model in which the general logic is implemented
and BookProduct CdProduct is already inherited from it, respectively, no MyProduct objects ever exist, there is no table for it,
or the general logic can be made a trait, but it seems to me more correct to do inheritance here

E
Evgeny Kylin, 2019-11-18
@palkan_karabov

I would do this:
Tables: cds, books, products. I would add 1k1 connections to them.
According to the models: Cd, Book, Product. Cd and Book have a belongsTo relationship to Product

C
cybersnail, 2019-11-18
@cybersnail

Why inherit something? You just need to implement additional fields for products, see how all stores are written, they allow you to create any products with any additional options.

D
Dmitry, 2019-11-21
@dlnsk

Your question is a tracing-paper from already set earlier. In reality, they appear about once a month... ;)
My answer to it: What is the best way to implement such an architecture?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question