简体   繁体   中英

How to provide mapping info in bundle but allow Entity class to be overriden

I've been developing applications in Symfony for a couple years now but haven't yet done a deep dive into independent bundle development, and it's biting me in the butt at the moment.

What I'm trying to do is provide some pre-defined entities which consuming applications can install but then override with their own entity classes if needs be.

Here are the two tables I'm providing with my bundle

src/My/CustomBundle/Resources/config/doctrine/User.orm.xml

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="My\CustomBundle\Entity\User" table="user">
    <id name="id" type="integer" column="id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="username" type="string" column="username" length="16" nullable="false"/>
    <field name="email" type="string" column="email" length="255" nullable="true"/>
    <one-to-many field="properties" target-entity="My\CustomBundle\Entity\UserProperty" mapped-by="user" fetch="EAGER" />
  </entity>
</doctrine-mapping>

src/My/CustomBundle/Resources/config/doctrine/User.orm.xml

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="My\CustomBundle\Entity\UserProperty" table="user_property">
    <indexes>
      <index name="fk_user_property_user1_idx" columns="user_id"/>
    </indexes>
    <unique-constraints>
      <unique-constraint columns="user_id,key" name="user_key_UNIQUE" />
    </unique-constraints>
    <id name="id" type="integer" column="id">
      <generator strategy="IDENTITY"/>
    </id>
    <field name="key" type="string" column="`key`" length="45" nullable="false"/>
    <field name="value" type="string" column="`value`" length="255" nullable="true"/>
    <many-to-one field="user" target-entity="My\CustomBundle\Entity\User" inversed-by="properties">
      <join-column name="user_id" referenced-column-name="id" nullable="false"/>
    </many-to-one>
  </entity>
</doctrine-mapping>

I just used the tools to auto-generate Entity classes from these mappings, so I won't bother posting the code for those because they're very predictable.

The problem then comes when I try to use these entities from another bundle. For example.

src/AppBundle/Entity/User.php

<?php

namespace AppBundle\Entity;

use My\CustomBundle\Entity\User as BaseUser;

class User extends BaseUser {}

Now this always prompts the error

[Doctrine\ORM\Mapping\MappingException]                                                                                           
  Class "AppBundle\Entity\User" sub class of "My\CustomBundle\Entity\User" is not a valid entity or mapped super class. 

If I try to inform doctrine that AppBundle\\Entity\\User is an entity, like so

<?php

namespace AppBundle\Entity;

use My\CustomBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;

/**
 * User
 *
 * @ORM\Entity
 */
class User extends BaseUser {}

Then this also fails

[Doctrine\DBAL\Schema\SchemaException]              
  The table with name 'database.user' already exists.

Is there a sane way for a bundle to provide pre-mapped entites but also allow for the consuming application to override them as needed?

Version Info

  • PHP 5.4.16
  • Symfony 2.6.4
  • Doctrine (Common) 2.4.2

If you're using doctrine, you need to either use single table inheritance, or a mapped supperclass.

here are the reference docs:

Single Table Inheritance

Mapped Superlcass

Note that doctrine has other types of inheritance, but they don't exactly apply to this use case. They are:

Class Table Inheritance

Overrides

If your use case doesn't fall into one of those categories you basically need to create your own entity from scratch and handle all the associations, mappings, etc on your own.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM