I have different type of users containing different attributes, namely
1. Customer, fields=[name, email]
2. Professional, fields=[name, email, region, locations, categories, timeslots]
3. Admin, fields=[name, email]
I expect few other type of users to be added later. I am using single table inheritance from doctrine, here is my classes.
1. User.php
namespace UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* @ORM\Entity
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\Table(name="user")
* @ORM\DiscriminatorColumn(name="discr", type="string")
* @ORM\DiscriminatorMap({"user" = "UserBundle\Entity\User", "customer"="UserBundle\Entity\Customer", "professional"="UserBundle\Entity\Professional", "admin"="UserBundle\Entity\Admin"})
* @ORM\Entity(repositoryClass="UserBundle\Entity\UserRepository")
*/
class User implements UserInterface
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="name", type="string", length=100, nullable=false)
*/
private $name;
/**
* @ORM\Column(name="email", type="string", length=100, unique=true, nullable=false)
*/
private $email;
}
2. Professional.php
<?php
namespace UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use UserBundle\Entity\User;
/**
* @ORM\Entity
*/
class Professional extends User
{
/**
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\Region", inversedBy="professionals", cascade={"persist"})
* @ORM\JoinColumn(name="region_id", referencedColumnName="id", unique=false, nullable=true)
*/
private $region;
/* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Location")
* @ORM\JoinTable(name="professional_location",
* joinColumns={@ORM\JoinColumn(name="professional_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="location_id", referencedColumnName="id", unique=false)}
* )
*/
private $locations;
/* @ORM\ManyToMany(targetEntity="AppBundle\Entity\Category")
* @ORM\JoinTable(name="professional_category",
* joinColumns={@ORM\JoinColumn(name="professional_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id", unique=false)}
* )
*/
private $categories;
/**
* @ORM\OneToMany(targetEntity="UserBundle\Entity\Timeslot", mappedBy="professional", cascade={"persist"})
*/
private $timeslots;
3. Customer.php
<?php
namespace UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use UserBundle\Entity\User;
/**
* @ORM\Entity
*/
class Customer extends User
{
public function getRoles()
{
return ['ROLE_CUSTOMER'];
}
}
The problem is, when I create the schema, it ignores the creation of associations in Professional Entity, for example table for locations and categories are not created, however region association works.
I understand that I can use @MappedSuperclass, the problem with this approach is duplication, this creates 3 tables with similar columns which I am trying to avoid.
How do I make many-to-many associations work with single table inheritance in doctrine2.
Thanks you.
I solved this, seems to be an issue with syntax used for many-to-many, the above syntax works without inheritance, but won't with inheritance, hence I changed it to following in
Professional.php
And it created
professional_location
andprofessional_category
table with many-to-many association.