I am beginning to work with Symfony, I’ve found some documentation about inheritance. But also found this discouraging article, which make me doubt if Doctrine handles inheritance any good at all…
Has anyone find a smart solution for inheritance in Symfony+Doctrine?
As an example, I have already structured the database something like this:
CREATE TABLE `poster` (
`poster_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(50) NOT NULL,
PRIMARY KEY (`poster_id`),
UNIQUE KEY `id` (`poster_id`),
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
CREATE TABLE `user` (
`user_id` int(11) NOT NULL,
`real_name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`user_id`),
UNIQUE KEY `user_id` (`user_id`),
CONSTRAINT `user_fk` FOREIGN KEY (`user_id`) REFERENCES `poster` (`poster_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
From that, Doctrine generated this “schema.yml”:
Poster:
connection: doctrine
tableName: poster
columns:
poster_id:
type: integer(4)
fixed: false
unsigned: false
primary: true
autoincrement: true
user_name:
type: string(50)
fixed: false
unsigned: false
primary: false
notnull: true
autoincrement: false
relations:
Post:
local: poster_id
foreign: poster_id
type: many
User:
local: poster_id
foreign: user_id
type: many
Version:
local: poster_id
foreign: poster_id
type: many
User:
connection: doctrine
tableName: user
columns:
user_id:
type: integer(4)
fixed: false
unsigned: false
primary: true
autoincrement: false
real_name:
type: string(50)
fixed: false
unsigned: false
primary: false
notnull: false
autoincrement: false
relations:
Poster:
local: user_id
foreign: poster_id
type: one
User creation for this structure with Doctrine auto-generated forms does not work.
Any clue will be appreciated.
After several months working on a project using both column aggregation and concrete inheritance I can only say one thing : stay away from concrete inheritance ! Really.
Let’s say you have 3 table : Media and Video/Audio which inherits from Media. You would expect to be able to do something like :
Well it won’t work with concrete inheritance. It’s just worthless and has almost no practical use, except for model inheriting methods.
On the other hand, with column aggregation, the Media table will have a “type” column automagically added, and will enable you to things such as :
Which will return a Video object collection. But you could also do this :
And you will get a mixed result of Video and Audio objects.
Anyway you should check the doctrine documentation.
But be careful as inheritance and doctrine can rapidly become troublesome.