Carl Boisvert Carl Boisvert - 7 months ago 47
PHP Question

Doctrine 2 class heritance mapping

I'm trying to figure out how to properly map my entities so I can make Doctrine 2 Class Table Inheritance (Doc here)

So I tried many different solutions, but I can't seem to validate my model with the command line tool (doctrine:schema:validate)

So here's what I've try. I haven't change the parent class so I'll leave it here.

Api\TestBundle\Entity\Item:
type: entity
table: items
inheritanceType: JOINED
discriminatorColumn:
name: type
type: string
nullable: false
length: 32
fixed: false
comment: ''
discriminatorMap:
item_property_facebook: ItemPropertyFacebook
id:
id:
type: integer
nullable: false
unsigned: false
comment: ''
id: true
generator:
strategy: AUTO
fields:
bundledType:
type: string
nullable: false
length: 32
fixed: false
comment: ''
column: bundled_type
lifecycleCallbacks: { }


And for the child class, I've put another id at first:

Api\TestBundle\Entity\ItemPropertyFacebook:
type: entity
table: item_property_facebook
id:
id:
type: integer
nullable: false
unsigned: false
comment: ''
id: true
generator:
strategy: AUTO
fields:
account_id:
type: integer
lifecycleCallbacks: { }


Obviously, I get:

Duplicate definition of column 'id' on entity 'Api\TestBundle\Entity
\ItemPropertyFacebook' in a field or discriminator column mapping.


So I've try to remove the id definition in the child, and I get:

No identifier/primary key specified for Entity "Api\TestBundle\Entit
y\ReferenceItemPropertyFacebook". Every Entity must have an identifier/prim
ary key.


So I absolutely have no clue. In the doc they just set up the basic things but not the id, and I can't find anything on the net because I probably don't formulate it well. I read about associationKey, but when I've tried it, Doctrine tell me the same

No identifier/primary key specified for Entity "Api\TestBundle\Entit
y\ReferenceItemPropertyFacebook". Every Entity must have an identifier/prim
ary key.


So if you have any clue, I would appreciate it. Thanks a lot!

Answer

I am not familiar with the yaml generation, but when I tried your structure with php annotations, it works for me, no problem with the duplicated id column:

/**
 * @ORM\Entity
 * @ORM\Table(name="items")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({"item" = "Item", "item_property_facebook" = "ItemPropertyFacebook"})
 */
class Item {

    /**
     * @ORM\Column
     * @ORM\Id
     * @ORM\GeneratedValue()
     */
    protected $id;

    /**
     * @ORM\Column(type="string")
     */
    protected $bundledType;
}

/**
 * @ORM\Entity
 * @ORM\Table(name="item_property_facebook")
 */
class ItemPropertyFacebook extends Item {

    /**
     * @var
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue()
     */
    protected $id;

    /**
     * @ORM\Column(type="integer")
     */
    protected $account_id;
}

Will generate those sql commands:

CREATE TABLE items (id VARCHAR(255) NOT NULL, bundledType VARCHAR(255) NOT NULL, type VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE item_property_facebook (id VARCHAR(255) NOT NULL, account_id INT NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE item_property_facebook ADD CONSTRAINT FK_B8C39352BF396750 FOREIGN KEY (id) REFERENCES items (id) ON DELETE CASCADE

The only difference was, I had to add "item = "Item" to DiscriminatorMap.

Also, should there be the id: true line in id definition? Seems redundant to me.

Comments