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