I am using the FOS_user
bundle and when I create a user I want to link it to another Entity which is Company. I have tried to create a Listener using the following code:
public static function getSubscribedEvents()
{
return array(
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
);
}
public function onRegistrationSuccess(FormEvent $event) {
}
This is working good and I have access to $event
in the function. Now, I want to access the name of the company that the user added in the form. Also, I want to create a company and bind it to the new user, so something like this:
$company = new Entity\Company();
$company->setTitle($theInputFromTheForm);
$user->addCompany($company);
I don't know how to access the data of the form and save the company in the user. Is using a listener the proper way to do it?
The event FOSUserEvents::REGISTRATION_SUCCESS
receive a FormEvent
object. You can get the registration form in your listener using $event->getForm()
So in you case;
public function onRegistrationSuccess(FormEvent $event) {
$registrationForm = $event->getForm();
$registrationFormData = registrationForm->getData();
}
I ended up creating a form type CompanyType
that adds the title of my company. In my RegistrationFormType
of my UserBundle
I added a collection to this form type. I created a listener on FOSUserEvents::REGISTRATION_INITIALIZE
to add an empty company.
public function onRegistrationInitialize(UserEvent $event) {
$user = $event->getUser();
$user->addCompany($company);
}
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.