0

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

Wilt
  • 41,477
  • 12
  • 152
  • 203
french_dev
  • 2,117
  • 10
  • 44
  • 85

1 Answers1

1

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"
})
Wilt
  • 41,477
  • 12
  • 152
  • 203
  • I understand now that `ParentUser.php` need and have to to be a base for other user class (`ParticularConsumer.php` and `ProfessionnalConsumer.php`). But as you understand, I need to extend the FOSUserBundle logic (registration, profile, connexion) to the child entity. How can I could proceed for that ? – french_dev Nov 19 '15 at 13:24
  • Like I said to make this work you should make `ParentUser` either to an Entity or a MappedSuperClass. Why do `ProfessionnalConsumer` and `ParticularConsumer` not extend `BaseUser` directly? – Wilt Nov 19 '15 at 13:34
  • I don't know if FOSUserBundle could be extends on many entities, I have not tried yet ? Is it possible ? – french_dev Nov 19 '15 at 13:42
  • @french_dev It is an abstract class. I would be surprised if it is not possible. But I have no first hand experience with it... – Wilt Nov 19 '15 at 13:54
  • Thank you for your help, of course it is an abstract class, I will tried to code what I need in extending the FOS two times. – french_dev Nov 19 '15 at 14:03
  • @french_dev Did it work our for you? Or still looking for alternatives? – Wilt Nov 23 '15 at 08:00
  • I am still looking for a better alternantive, I tried to extends the BaseUser of FOS on many users entities, but as you can see on the doc, FOSUserBundle is made fto extend the BaseUser on only one one entity. I am looking for something more appropriate to my application. If I found something better, don't worry I will post here some more informations. Thank you by the way. – french_dev Nov 23 '15 at 08:26
  • @french_dev And did you try my other suggestion: making `ParentUser` to an entity and adding it to the discriminator map? I think it will work like that too... Or make `ParticularConsumer` to parent and let `ProfessionnalConsumer` extend `ParticularConsumer` – Wilt Nov 23 '15 at 08:28
  • check this question on stack [here](http://stackoverflow.com/questions/21043055/fosuserbundle-or-pugxmultiuserbundle-to-have-two-different-user-profiles-symfon). This is not exactly what I need to, even if I need a `ParentUser`. But, I f I have not found another solution I will try yours like the link show. – french_dev Nov 23 '15 at 08:33