I'm using Doctrine 2.2.0 together with Codeigniter. I'm new to Doctrine (or to ORM in general).
I'm setting up entity and proxy classes based on YAML files which works fine. I do have problems in reflecting a polymorphic association in my DB in my Doctrine classes. I'm looking for a concrete example on how to implement the following polymorphic association in Doctrine.
In my DB I have a table called products. Depending on the value of field object_type
and object_id
I want to relate either to a record in the table videos or the table cars (I simplified it here). I want to keep both product types in 2 separate table because one has nothing to do with the other and both tables relate to other tables.
I took a look at the Doctrine Inheritance documentation and other examples, but it doesn't seem to help me. If possible I want to avoid adding the columns description
and price
to the tables videos and cars.
Many thanks!
|Table: products |
|-----------------------------------------------------|
| ID | description | price | object_type | object_id |
|-----------------------------------------------------|
| 1 | A video | 20.00 | video | 12 |
| 2 | A car | 159.00 | car | 5 |
|Table: videos |
|--------------------------------------------|
| ID | filename | artist_id | date |
|--------------------------------------------|
| 12 | somename.mp4 | 189 | 2011-02-15 |
|Table: cars |
|------------------------------|
| ID | brand_id | model | year |
|------------------------------|
| 5 | 17 | astra | 2010 |
You were looking in the right place, inheritance mapping is certainly what you need to achieve this. From what I can tell this is a perfect example of "class table inheritance".
Check out the manual for some example / explanation on how to implement this.
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/inheritance-mapping.html#class-table-inheritance
Found the solution myself. I've listed below the different steps I've done. This answer is very big, but I tried to be as complete as possible.
The most important things are in the Product YAML files
inheritanceType: JOINED
,discriminatorColumn:
,discriminatorMap:
and the Video and Car entity classesclass Video extends Product
,class Car extends Product
.1) DB Schema
2) Doctrine YAML files (from which you need to create the Entity and proxy classes)
Entities.Artist.dcm.yml
Entities.Brand.dcm.yml
Entities.Car.dcm.yml
Entities.Product.dcm.yml
Entities.Video.dcm.yml
3) Create Entity and Proxy classes
I use CodeIgniter. I've used Joel Verhagen's excellent guide
!! It's important you extend Video and Car with Product - See below !!
This results in the following Entity classes
Artist.php
Brand.php
Car.php
Product.php
Video.php
3) Creating a new Car and a new Video
This is what goes in my CodeIgniter controller. The code presumes you've created an artist with the name Metallica and a brand with the name Ford.
4) Extracting all products
An example on how to extract all Products
This results into
An example onn how to extract all Videos
This results into