Vik Verma Vik Verma - 6 months ago 25
MySQL Question

Symfony2 save two forms in one action

I'm new symfony so got stuck in this issue.

I'm saving two different forms in once action. For instance : first i'm saving user detail than get the last inserted id to save another detail like : user hours.

My first form is inserting but second one is not saving the data. Here is my code:

$user = new Users ();
$user->setName($strUserName);
$user->setAddress1($strAddress1);
$user->setAddress2($strAddress2);
$user->setCity($strCity);
$user->setState($strState);
$user->setZip($strZip);
$user->setContactSales($strContactSales);
$user->setContactService($strContactService);
$user->setContactParts($strContactParts);
$user->setWebsite($strWebsite);

$em = $this->getDoctrine()->getManager();
$em->persist($user);

$userId = $user->getId();

$userWorkingHours = new UserWorkingHours();
$userWorkingHours->setDay = '"' . $day . '"';
$userWorkingHours->setStartDay = '"' . $startDay . '"';
$userWorkingHours->setEndDay = '"' . $endDay . '"';
$userWorkingHours->setLunchHourStart = '"' . $lunchStart . '"';
$userWorkingHours->setLunchHourEnd = '"' . $lunchEnd . '"';
$userWorkingHours->setUserId = '"' . $userId . '"';
$userWorkingHours->setIsActive = '"' . $isActive . '", ';
$userWorkingHours->setCreatedOn = '"' . time() . '"';
$userWorkingHours->setType = 1;

$em->persist($userWorkingHours);
$em->flush();


Please give me the solution guys. I've been stuck here since morning. Thank you

Answer

always flush after persist

$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();

and i dont know what you are doing with these '"'

should be $userWorkingHours->setDay($day);