Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP MySQL table inheritance
    primarykey
    data
    text
    <p>I am implementing two objects, say HotDog and Burger, and they are both subclass of Food.</p> <p>The system I am working on will be used to create HotDog and Burger, potentially other types of Food, <strong>but never will it be used to created a generic "Food" object.</strong></p> <p>Food</p> <pre><code>class Food{ //some fields protected $cooked_by; protected $use_before_date; } </code></pre> <p>HotDog:</p> <pre><code>class HotDog extends Food{ //some fields specicifc to HotDog private $sauage_type; } </code></pre> <p>Burger</p> <pre><code>class Burger extends Food{ //some fields specicifc to Burger private $burger_type; } </code></pre> <p>These models have a corresponding DB Table, namely Food, HotDog and Burger.</p> <p>How if I want to select all HotDog cooked by a certain chief, currently I have to loop through every single row of HotDog like this:</p> <p><code>SELECT food_id FROM hotdog</code></p> <p>Then loop through the entire resultset, for each row doing:</p> <p><code>SELECT * FROM food WHERE food_id = x AND cooked_by=chief0001</code></p> <p>I hope you understand the structure I am trying to build here. Currently having to obtain all food_id for each HotDog row, then select from the food table one-by-one is a serious performance hit.</p> <p>It would be nice to be able to just do :</p> <p><code>SELECT * FROM hotdog WHERE cooked_by = chief0001</code></p> <p>However as you can see, the current way of implementing the Models and the Tables does not allow me to do this. Surely I can add another column to the HotDog and Burger tables, however, should I need to query anything other than cooked_by, I will have to add yet another column to both tables.</p> <p>Eventually, I will just add all the columns in the Food table into HotDog and Burger.</p> <p>Is that the right way to do table inheritance ? I feel like something isn't right here, and there should be a cleaner and better solution. However I am pulling my hair trying to come up with a good way of overcoming this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload