Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Association mapping with mapped superclass

In my vendor bundle, I have 2 mapped superclass : BaseSite and BaseSection (which are abstract).

In my application bundle, I have 2 entities that extends the 2 mapped superclass. Everything works so far: I have access to the fields defined in the superclasses and I can add new ones in my application bundle if needed.

The problem is when I am trying to define my association mapping between those entities. (manyToOne between BaseSection and BaseSite). If I define it in the BaseSection mapped superclass, I am able to run the command app/console doctrine:generate:entities AcmeDemoBundle, but it doesn't work when I try to create the tables: (app/console doctrine:schema:update --dump-sql)

CREATE TABLE Section (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, siteId INT DEFAULT NULL, INDEX IDX_95E06DEFFADB670C (siteId), PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE Site (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
ALTER TABLE Section ADD CONSTRAINT FK_95E06DEFFADB670C FOREIGN KEY (siteId) REFERENCES BaseSite(id) ON DELETE CASCADE

As you can see, it tries to reference the foreign key on a table that doesn't exists (BaseSite instead of Site). I'm guessing this is because the mapped superclass isn't aware of the table name defined in the application entity.

I could define the association mapping on the application entities instead, but that would mean that if someone wanted to use my bundle, he would have to define the mapping himself, which I would like to avoid.

Is there another way to do this or maybe I'm just missing something?

Here is my code:

Vendor :

File: vendor\bundles\Acme\DemoBundle\Resources\config\doctrine\BaseSite.orm.yml

Acme\DemoBundle\Entity\BaseSite:
  type: mappedSuperclass
  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
    name:
      type: string
      length: 255
      nullable: false
    // ...

File: vendor\bundles\Acme\DemoBundle\Resources\config\doctrine\BaseSection.orm.yml

Acme\DemoBundle\Entity\BaseSection:
  type: mappedSuperclass
  fields:
    id:
      type: integer
      id: true
      generator:
        strategy: AUTO
    name:
      type: string
      length: 255
      nullable: false
    // ...
  manyToOne:
    site:
      targetEntity: Acme\DemoBundle\Entity\BaseSite
      joinColumn:
        name: siteId
        referencedColumnName: id
        onDelete: cascade

Application:

File: src\Application\Acme\DemoBundle\Resources\config\doctrine\Site.orm.yml

Application\Acme\DemoBundle\Entity\Site:
  type: entity
  table: Site

File: src\Application\Acme\DemoBundle\Entity\Site.php

<?php

namespace Application\Acme\DemoBundle\Entity;

use Acme\DemoBundle\Entity\BaseSite;

class Site extends BaseSite
{
}

File: src\Application\Acme\DemoBundle\Resources\config\doctrine\Section.orm.yml

Application\Acme\DemoBundle\Entity\Section:
  type: entity
  table: Section

File: src\Application\Acme\DemoBundle\Entity\Section.php

<?php

namespace Application\Acme\DemoBundle\Entity;

use Acme\DemoBundle\Entity\BaseSection;

class Section extends BaseSection
{
}
like image 785
Emilie Avatar asked Mar 26 '12 22:03

Emilie


1 Answers

After reading the Doctrine manual on Inheritance Mapping its says:

This means that One-To-Many associations are not possible on a mapped superclass at all

It might be worth looking at the table inheritance features.

like image 86
Lee Davis Avatar answered Nov 15 '22 18:11

Lee Davis