Ejemplo n.º 1
0
 public function load(ObjectManager $manager)
 {
     $users = array('pepe' => array('name' => 'Pepe', 'addresses' => array(array('street' => 'Calamar 23', 'city' => 'valencia'), array('street' => 'Perico 3', 'city' => 'lagos'))), 'juan' => array('name' => 'Juan', 'addresses' => array(array('street' => 'Castellana 45', 'city' => 'madrid'))), 'manuel' => array('name' => 'Manuel', 'addresses' => array(array('street' => 'Principal 94', 'city' => 'fafe'))));
     foreach ($users as $username => $user) {
         $newUser = new User();
         $newUser->setUsername($username);
         $profile = new Profile();
         $profile->setName($user['name']);
         $newUser->setProfile($profile);
         $addresses = new ArrayCollection();
         foreach ($user['addresses'] as $address) {
             $newAddress = new Address();
             $newAddress->setStreet($address['street']);
             $city = sprintf('city-%s', $address['city']);
             $newAddress->setCity($this->getReference($city));
             $addresses->add($newAddress);
         }
         $newUser->setAddresses($addresses);
         $manager->persist($newUser);
     }
     $manager->flush();
 }
Ejemplo n.º 2
0
 /**
  * @Route("/forms/user/new", name="examples_forms_user_create")
  * @Template("MainBundle:Example\Form:new_user.html.twig")
  */
 public function userNewAction(Request $request)
 {
     $user = new User();
     $profile = new Profile();
     $user->setProfile($profile);
     $form = $this->createForm(new UserType(), $user);
     if ($request->isMethod("POST")) {
         $form->bind($request);
         if ($form->isValid()) {
             $em = $this->getDoctrine()->getManager();
             $em->persist($user);
             // $em->flush();
             $flashBag = $this->get('session')->getFlashBag();
             $flashBag->add('smtc_success', 'Se ha creado un usuario:');
             $flashBag->add('smtc_success', sprintf('Username: %s', $user->getUsername()));
             $flashBag->add('smtc_success', sprintf('Nombre: %s', $user->getProfile()->getName()));
             if (0 !== count($user->getAddresses())) {
                 $flashBag->add('smtc_success', 'Direcciones:');
                 foreach ($user->getAddresses() as $address) {
                     $flashBag->add('smtc_success', sprintf('  %s (%s)', $address->getStreet(), $address->getCity()->getName()));
                 }
             }
             return $this->redirect($this->generateUrl('examples_forms'));
         }
     }
     return array('form' => $form->createView());
 }