I need to merge table 'fos_user' and 'institution'.And I need to display registration form from both entities. I have a problem with FOSUserBundle. I created new properties in User class
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
protected $workPhone;
protected $adminPhone;
protected $name;
protected $adress;
public function __construct()
{
parent::__construct();
}
public function setAdress($adress)
{
$this->adress = $adress;
}
public function setName($name)
{
$this->name = $name;
}
public function setWorkPhone($workPhone)
{
$this->workPhone = $workPhone;
}
public function setAdminPhone($adminPhone)
{
$this->adminPhone = $adminPhone;
}
public function getName()
{
return $this->name;
}
public function getAdress()
{
return $this->adress;
}
public function getWorkPhone()
{
return $this->workPhone;
}
public function getAdminPhone()
{
return $this->adminPhone;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
And I have entity Institution, which I want merge.
/**
* @var integer
*
* @ORM\Column(name="id_institution", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idInstitution;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=45, nullable=false)
*/
private $name;
/**
* @var integer
*
* @ORM\Column(name="work_phone", type="integer", nullable=false)
*/
private $workPhone;
/**
* @var integer
*
* @ORM\Column(name="admin_phone", type="integer", nullable=true)
*/
private $adminPhone;
/**
* @var string
*
* @ORM\Column(name="adress", type="string", length=255, nullable=false)
*/
private $adress;
/**
* @var \AppBundle\Entity\FosUser
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\FosUser")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="id_fos_user", referencedColumnName="id")
* })
*/
private $idFosUser;
/**
* Get idInstitution
*
* @return integer
*/
public function getIdInstitution()
{
return $this->idInstitution;
}
/**
* Set name
*
* @param string $name
* @return Institution
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set workPhone
*
* @param integer $workPhone
* @return Institution
*/
public function setWorkPhone($workPhone)
{
$this->workPhone = $workPhone;
return $this;
}
/**
* Get workPhone
*
* @return integer
*/
public function getWorkPhone()
{
return $this->workPhone;
}
/**
* Set adminPhone
*
* @param integer $adminPhone
* @return Institution
*/
public function setAdminPhone($adminPhone)
{
$this->adminPhone = $adminPhone;
return $this;
}
/**
* Get adminPhone
*
* @return integer
*/
public function getAdminPhone()
{
return $this->adminPhone;
}
/**
* Set adress
*
* @param string $adress
* @return Institution
*/
public function setAdress($adress)
{
$this->adress = $adress;
return $this;
}
/**
* Get adress
*
* @return string
*/
public function getAdress()
{
return $this->adress;
}
/**
* Set idFosUser
*
* @param \AppBundle\Entity\FosUser $idFosUser
* @return Institution
*/
public function setIdFosUser($idFosUser = null)
{
$this->idFosUser = $idFosUser;
return $this;
}
/**
* Get idFosUser
*
* @return \AppBundle\Entity\FosUser
*/
public function getIdFosUser()
{
return $this->idFosUser;
}
This is pert of InstitutionManager where I want save Entity, and this is like service now:
public function createNewEntity($user)
{
$entity = new Institution();
$entity->setName($user->getName());
$entity->setAdminPhone($user->getAdminPhone());
$entity->setWorkPhone($user->getWorkPhone());
$entity->setAdress($user->getAdress());
$entity->setidFosUser($user->getId());
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
}
And hear override RegistrationController:
public function registerAction()
{
$form = $this->container->get('fos_user.registration.form');
$formHandler = $this->container->get('fos_user.registration.form.handler');
$confirmationEnabled = $this->container->getParameter('fos_user.registration.confirmation.enabled');
$process = $formHandler->process($confirmationEnabled);
if ($process) {
$user = $form->getData();
$authUser = false;
if ($confirmationEnabled) {
$this->container->get('session')->set('fos_user_send_confirmation_email/email', $user->getEmail());
$route = 'fos_user_registration_check_email';
} else {
$authUser = true;
$route = 'fos_user_registration_confirmed';
}
$this->setFlash('fos_user_success', 'registration.flash.user_created');
$institutionManager = $this->container->get('institution_manager');
$institution = $institutionManager->createNewEntity($user);
$url = $this->container->get('router')->generate($route);
$response = new RedirectResponse($url);
if ($authUser) {
$this->authenticateUser($user, $response);
}
return $response;
}
return $this->container->get('templating')->renderResponse('FOSUserBundle:Registration:register.html.'.$this->getEngine(), array(
'form' => $form->createView(),
));
}
services.yml:
services:
institution_manager:
class: AppBundle\Lib\Manager\InstitutionManager
arguments: [ @doctrine.orm.default_entity_manager ]