php - How to specify foreign key column for Class Table Inheritance in Doctrine2? -


how can specify columns used foreign key relation class table inheritance in doctrine 2? example, take following 2 classes:

/**  * @entity  * @inhertancetype("joined")  * @discriminatorcolumn(name="type", type="string")  * @discriminatormap("person" = "person", "employee" = "employee")  */ class person {     /** @id */     public $id;      /** @column(type="string") */     public $ssn; }  /** @entity */ class employee {     /** @column(type="decimal") */     public $salary; } 

with this, doctrine expects table structure along this:

create table `person` (     `id` int(11) not null auto_increment,     `ssn` varchar(255) default null,     primary_key(`id`) )  create table `employee` (     `person_id` int(11) not null,     `salary` decimal(10,2) default null,     primary_key(`person_id`) ) alter table `employee`     add constraint `person_fk` foreign key (`person_id`)         references `person` (`id`) on delete cascade 

there foreign key employee.person_id points person.id. how can tell doctrine columns use? assume person.id reference comes @id annotation on person class, if want create fk person.ssn instead (it's conceivable, since ssn naturally unique).

and if have legacy database employee.person_id called employee.p_id instead?

first of all, missing "extends person" in employee class definition.

from know, when doing type of inheritance, doctrine uses primary key in main class join other tables, expected behavior.

when query, doctrine join child tables parent , hydrate according discriminator map. in mind, doesn't matter if primary key automatically incremental id or unique field. if have ssn defined in parent class, available searching in subclasses.

so couple of tips here. can if want, remove automatical id , use ssn primary key, doctrine expects of child tables have same field defined in them. performance may wise have integer instead of 255 string joins anyway.

if want mantain automated id, may want add unique index parent class, way if access classes field, wont performance slowdowns.

if want have class name , table name else, use this

/** @id @column(name="p_id") */ public $id; 

but remember tables part of inheritance should use name.

moreover, use doctrine mapping existing database , extending (migrate doctrine), , if i'm adding new feature , model requires so, create mapping , tables myself keeping in mind how doctrines inheritance works. if have existing tables think can modeled inheritance, expect trouble, , maybe needs modify existing tables.

hope helps.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -