简体   繁体   中英

persist parent and child entities using FOSUserBundle and “JOINED” Class Table Inheritance Doctrine 2/Symfony2.7

I am using the FOSUserBundle in order to manage my users into my application. But in fact, I have multiple user entities: ParticularConsumer.php and ProfessionnalConsumer.php . So I create a ParentUser.php entity who as an abstract class who extends BaseUser of FOSUserBundle . See the code here:

/**
 * ParentUser
 *
 * @ORM\Table(name="parent_users")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({"particular_consumer" = "ParticularConsumer", "professionnal_consumer" = "ProfessionnalConsumer"})
 * @ORM\Entity(repositoryClass="MyBundle\EntityBundle\Repository\ParentUserRepository")
 *
 */
abstract class ParentUser extends BaseUser
{
/**
     * @ORM\Id
     * @ORM\Column(name="pusr_id", type="integer", nullable=false)
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

    // ...

So there are the two other entities who extend the Parentuser.php , following the Class Table Inheritance of Doctrine behavior and documentation:

First ParticularConsumer.php

/**
 * ParticularConsumer
 *
 * @ORM\Table(name="particular_consumer")
 * @ORM\Entity(repositoryClass="MyBundle\EntityBundle\Repository\ParticularConsumerRepository")
 *
 */
class ParticularConsumer extends ParentUser
{

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

}

Second ProfessionnalConsumer.php

/**
 * ProfessionnalConsumer
 *
 * @ORM\Table(name="professionnal_consumer")
 * @ORM\Entity(repositoryClass="MyBundle\EntityBundle\Repository\ProfessionnalConsumerRepository")
 *
 */
class ProfessionnalConsumer extends ParentUser
{

    public function __construct()
    {
        parent::__construct();
        // your own logic
    }

}

Now, that I would like to do and to know is how to persist the parent and child entities.

Indeed, as I use the FOSUserBundle , all the routes (register,login, etc...) are managed and generated by this bundle. Now I need to persist datas in Child entities, normally it persists datas in parent entity, that's right ? How can I proceed exactly ?


This how I need to proceed to register a consumer:

There is a question on a page, whish ask users if there are professionals or particulars.

A drop down list is here for them to make the choice.

Following the choice, I need to register the user in the right entity. If the user choose particular in the drop down list, I need to load/display a form to persist data in ParticularConsumer.php and not only in ParentUser.php .

But as I use the FOSUSerBundle, I don't really know how to proceed exactly. As you can understand, using the FOS is a practical way in order to manage users and manage the security, I would like to keep the logic of the bundle. And I want to use the good practices.

Finally, following all the doc of the FOSUserBundle in order to install it, if I want to register a user ( localhost/web/app_dev.php/register ) I have this error:

Error: Cannot instantiate abstract class MyBundle\\EntityBundle\\Entity\\ParentUser

If your ParentUser is an abstract class then it cannot be an Entity . In such case you have to make it to a MappedSuperClass . But as you can read in the documentation:

A mapped superclass cannot be an entity, it is not query-able and persistent relationships defined by a mapped superclass must be unidirectional (with an owning side only). This means that One-To-Many associations are not possible on a mapped superclass at all. Furthermore Many-To-Many associations are only possible if the mapped superclass is only used in exactly one entity at the moment. For further support of inheritance, the single or joined table inheritance features have to be used.

If you want to be able to query your ParentUser and you want this class to have its own entity repository then you will have to remove abstract and add a value for ParentUser to your @ORM\\DiscriminatorMap definition:

@ORM\DiscriminatorMap({
    "parent_user" = "ParentUser"
    "particular_consumer" = "ParticularConsumer",
    "professionnal_consumer" = "ProfessionnalConsumer"
})

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