I have a main User
entity that holds common fields but also has some extensions like for example Company
and Natural
both extends from User
. Basically (I remove the rest of info since it's not relevant here) this is how my entities looks:
\UserBundle\Entity\User.php
:
class User extends BaseUser
{
/**
* @var \Natural
*
* @ORM\OneToMany(targetEntity="\UserBundle\Entity\Natural", mappedBy="user")
*/
protected $natural;
/**
* @var \Company
*
* @ORM\OneToMany(targetEntity="\UserBundle\Entity\Company", mappedBy="user")
*/
protected $company;
public function __construct()
{
parent::__construct();
$this->natural = new ArrayCollection();
$this->company = new ArrayCollection();
}
/**
* Get natural
*
* @return \UserBundle\Entity\Natural
*/
public function getNatural()
{
return $this->natural;
}
}
\UserBundle\Entity\Company.php
:
class Company
{
/**
* @ORM\Column(name="social_reason", type="string", length=200, nullable=false)
*/
protected $social_reason;
/**
* @var \User
*
* @ORM\ManyToOne(targetEntity="\UsuarioBundle\Entity\User", inversedBy="company")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* Get razon social
*
* @return decimal
*/
public function getSocialReason()
{
return $this->social_reason;
}
/**
* Set User
*
* @param \UserBundle\Entity\User $user
* @return Company
*/
public function setUser(\UserBundle\Entity\User $user = NULL)
{
$this->user = $user;
return $this;
}
/**
* Get User
*
* @return \UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}
\UserBundle\Entity\Natural.php
:
class Natural
{
/**
* @ORM\Column(name="name", type="string", length=150)
*/
protected $name;
/**
* @ORM\Column(name="lastname", type="string", length=200)
*/
protected $lastname;
/**
* @var \User
*
* @ORM\ManyToOne(targetEntity="\UserBundle\Entity\User", inversedBy="company")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
/**
* Set User
*
* @param \UserBundle\Entity\User $user
* @return Empresa
*/
public function setUser(\UserBundle\Entity\User $user = NULL)
{
$this->user = $user;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Get lastname
*
* @return string
*/
public function Lastname()
{
return $this->lastname;
}
/**
* Get User
*
* @return \UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
}
Having this relationship between entities, how in FOSUserBundle:Registration:email.txt.twig
(which is override by me at \app
) I can access methods from Natural
entity or Company
entity? Let's say I need from Natural
the name getName
and lastname getLastName
? Why? Because I need to send extra info on the email sended to Users and that info is stored on that tables when Users got registered, any help?
Clarifying some things
-
User
: is a system User from FOSUserBundle -
Company
andNatural
: both are extensions of User the difference is which kind of information stores each one, for example a Company may have Social Reason, RIF (some kind of ID number), How many people works on that company, if it's a Regional Company or a main Company and Contact Person (this is not a system user is a normal entity linked just to Company) and a Natural may have a CI (some kind of ID number like DNI on European countries), Name, Lastname, Birthday, Sex and so on. Both Natural and Company shares some properties as for example username, email (canonicalEmail), address, Phone Number and some others, for that reason Company and Natural are linked to User and they can not exists if a User does not exists before
So the process here is:
- Create the user and persist it using UserManager
- Create a Natural/Company entity and set their properties from data in $request and yes this has to be done by hand for that you'll see
setUser()
(which set the current user created before as the FK for Natural/Company) and some others - Handle the relationship in a N:M entity UserHasService()