0

I have a mysql database diagram / structure like this: enter image description here Table Item as parent, and have 3 child table (ItemArticle, ItemElement, and ItemTool).

Every child should have parent parameter (warehouse_id and item_type_id). I am planning to connect table Item to children with item_id

How I make PHP class inheritance with this kind of database structure?

For example, if I make method getByItemId() then run the method, then PHP should return the class according to the item_type_id (If the item_type is article, then new ItemArticle())

I'm using Laravel framework.

EDIT: Earlier comments and suggestions from you, I think miss understanding from what I've been asking. So, please have a minute look at this link PHP Inheritance and MySQL

I wanted to have something like the checked mark answer. But don't know how to implement in my case.

5
  • 1
    "Extends" is not a very accurate description here, if this is a DB diagram. These are one-to-many relationships, I think? Each of the ItemXXXX tables has its own ID, so I'm assuming that item_id is not the primary key of those tables? Therefore multiple rows with the same item_id can exist? If so then these are separate entities. The item_id is just a property which references an item to which that instance of the entity has a relationship. You wouldn't reflect that in code with an inheritance structure, you'd have totally separate classes. Commented Sep 19, 2017 at 14:35
  • If this is not the case and item_id must be unique in each of these tables, then it should be the primary key of the table, and the "id" field would not be needed. In that case though I'd question whether you can just have one table instead of 4, and leave some of the fields null in any given row if they're not needed for that item_type. Your application layer can handle validating that the necessary fields are populated for each type. Commented Sep 19, 2017 at 14:36
  • If it is a school test you should create your tables (either three tbales or one table with a column type to make difference between items) then you can do Inheritance in your PHP as you learned in OOP, and then you need to map your classes to the database tables. If it's not about school, use an ORM, it will do the job for you (Doctrine, Eloquent, ...) Commented Sep 19, 2017 at 14:37
  • @ADyson Yes, this is a DB Diagram, but also I wanted to reflecting what PHP classes i wanted. Extends in the diagrams means ItemArticle extends Item Commented Sep 19, 2017 at 14:46
  • @Erick "extends" is not really a meaningful concept in a DB Entity-relationship diagram as I understand it, it's an OO concept. That's what I was trying to say. Can you answer my question about the foreign key relationships? If so then it might help to point you in the right direction. As I wrote, there are two possible scenarios for how you structure your code, depending on what the rules are (one-to-one or one-to-many). Commented Sep 19, 2017 at 14:51

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.