mercredi 25 octobre 2017

Should I use polymorphic relationships when using different columns by entity?

I have a few tables that I'm unsure if it makes sense to use polymorphic relationships, because in one of them there are different columns/attributes.
(Note: I'm building in laravel 5.5)

Here is a simplified version of the tables:

pick_up_points, shipping_entities, products
  id
  name

pick_up_points_periods
  id
  pick_up_point_id
  week_day // -> a number matching the day of the week (0->Monday, ..., 6->Sunday)
  start_time // ex: "24:59"
  end_time

shipping_entities_delivery_periods  // (the shipping entities follow the same delivery pattern between them)
  id
  shipping_entity_id
  week_day
  start_time
  end_time

product_promotion_periods
  id
  product_id
  start_date // ex: "2017-10-25"
  end_date


I was thinking in 2 possible ways to use polymorphic relationships.

Case 1 - create the table periods just to replace the tables pick_up_points_periods and shipping_entities_delivery_periods

periods
  id
  periodable_type
  week_day
  start_time
  end_time

In this case (1) it makes more sense to me, since they use the same fields/columns, but it would leave another periods table out.


Case 2 - create the table periods to replace all *_periods tables (tables pick_up_points_periods, shipping_entities_delivery_periods and product_promotion_periods)

periods
  id
  periodable_id
  periodable_type
  week_day
  start_time
  end_time
  start_date
  end_date

in this case (2) it makes less sense to me, since they use different fields/columns, but it would fit all periods context tables together (by reducing even more the number of tables). . and there would be records with unused columns if they were for example pick_up_points_periods or product_promotion_periods.


My line of thought:

In the laravel v5.5 documentation here, there are 2 examples of polymorphic relationships for:

  • comments - that exist for both posts and videos
  • tags - that exist for posts and videos

For me, both comments and tags are entities that make sense to be polymorphic, because they have always the same shared fields across the other entities (posts, videos or any other i can think off)

However, for periods I can think of many kinds of them, like for example:

  • time period of a day of the week
  • time period (of every day)
  • date period ([start_date:'2017-02-28', end_date:'2017-05-31'])
  • timestamp period (ex: [start_date:'2017-02-28 08:00:00', end_date:'2017-05-31 20:00:00'])
  • weeks of the month (example: only at the 1st 2 weeks of each month)
  • n week day of the month (example: only at the 3rd Tuesday of each month)
  • a mix of them ([start/end]_[time/date/timestamp], week_day, month_[day/week]), ...

where each entity / table could have different attributes / columns even if they would still fit the "periods" context.

  • I could build a polymorphic table with all the fields (like case 2), but they could be empty/null depending on the periodable_type
    • but at least could also predict a change of the period pattern (/ fields used)
      • or just adding the new fields when needed even if not all types used

I tried to search for something like if there is some "best practices on when to use or not polymorphic relationships", but i could not properly find a similar situation.

So I would like to know if: should use case 1, case 2 or just not use polymorphic relations at all? what are the pros/cons of each one? Is there any best practices for this matter?

Thank you in advance.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2y5xeb5
via IFTTT

Aucun commentaire:

Enregistrer un commentaire