Q: Are 1:1 associations supposed to be mirrored in class table inheritances?
R3dLantern opened this issue · 0 comments
I use Sulu CMS and extend its User
entity as per docs. For my use case, I need to split the user into admin and non-admin user, which I did using class table inheritance:
/**
* @ORM\Table(name="se_users")
* @ORM\Entity
* @ORM\InheritanceType("JOINED")
* @ORM\DiscriminatorColumn(name="user_type",type="string",length=15)
* @ORM\DiscriminatorMap({
* "admin":"App\Entity\AdminUser",
* "nonadmin":"App\Entity\NonAdminUser"
* })
*/
abstract class User extends SuluUser
{
// ...
}
/**
* @ORM\Table(name="proj_admin_users")
* @ORM\Entity
*/
class AdminUser extends User
{
// ...
}
/**
* @ORM\Table(name="proj_nonadmin_users")
* @ORM\Entity
*/
class NonAdminUser extends User
{
// ...
}
Sulu\Bundle\SecurityBundle\Entity\User
is configured via .orm.xml
file, stating a 1:1 association to the table co_contacts
on its $contact
property:
<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">
<mapped-superclass name="Sulu\Bundle\SecurityBundle\Entity\User" table="se_users">
<!-- ... -->
<one-to-one field="contact" target-entity="Sulu\Bundle\ContactBundle\Entity\ContactInterface">
<join-column name="idContacts" referenced-column-name="id" on-delete="CASCADE" nullable="false"/>
</one-to-one>
<!-- ... -->
</mapped-superclass>
</doctrine-mapping>
When creating a new migration class, the SQL statements for creating proj_admin_users
and proj_nonadmin_users
contain not only an FK-referenced id
column, but also an FK-referenced idContacts
column that mirrors the column in se_users
:
$this->addSql('CREATE TABLE proj_admin_users (id INT NOT NULL, idContacts INT NOT NULL, UNIQUE INDEX UNIQ_AAAAAAAAAAAAAAAA (idContacts), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE proj_admin_users ADD CONSTRAINT FK_AAAAAAAAAAAAAAAA FOREIGN KEY (idContacts) REFERENCES co_contacts (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE proj_admin_users ADD CONSTRAINT FK_BBBBBBBBBBBBBBBB FOREIGN KEY (id) REFERENCES se_users (id) ON DELETE CASCADE');
However, fetching of the inheriting entities fails due to class metadata handling seemingly cancelling out their idContacts
columns.
Is the behaviour of creating mirroring 1:1 columns in class table inheritances an intended feature?