php - Registering formular that doesn't flush. Symfony 2.5.6 -
i have e little problem. followed tutorial create register formular doesn't persist entity. don't understand why. doesn't create mistake, just... doesn't flush.
here tutorial: http://symfony.com/fr/doc/2.5/cookbook/doctrine/registration_form.html
here entity:
namespace theia\mainbundle\entity; use doctrine\orm\mapping orm; use symfony\component\validator\constraints assert; use symfony\bridge\doctrine\validator\constraints\uniqueentity; /** * usermain * * @orm\table() * @orm\entity(repositoryclass="theia\mainbundle\entity\usermainrepository") */ class usermain { /** * @var integer * * @orm\column(name="id", type="integer") * @orm\id * @orm\generatedvalue(strategy="auto") */ private $id; /** * @var string * * @orm\column(name="email", type="string", length=255, unique=true) * @assert\notblank() * @assert\email() */ private $email; /** * @var string * * @orm\column(name="password", type="string", length=255) * @assert\notblank() */ private $password; /** * @var array * * @orm\column(name="roles", type="array") */ private $roles; /** * id * * @return integer */ public function getid() { return $this->id; } /** * set email * * @param string $email * @return usermain */ public function setemail($email) { $this->email = $email; return $this; } /** * email * * @return string */ public function getemail() { return $this->email; } /** * set password * * @param string $password * @return usermain */ public function setpassword($password) { $this->password = $password; return $this; } /** * password * * @return string */ public function getpassword() { return $this->password; } /** * set roles * * @param array $roles * @return usermain */ public function setroles($roles) { $this->roles = $roles; return $this; } /** * roles * * @return array */ public function getroles() { return $this->roles; } public function __construct() { $this->roles = [ 'role_user' ]; } }
here "security controller":
namespace theia\mainbundle\controller; use symfony\bundle\frameworkbundle\controller\controller; use sensio\bundle\frameworkextrabundle\configuration\route; use sensio\bundle\frameworkextrabundle\configuration\template; use symfony\component\security\core\securitycontextinterface; use symfony\component\httpfoundation\request; use theia\mainbundle\form\type\registrationtype; use theia\mainbundle\form\model\registration; class securitycontroller extends controller { public function loginaction(request $request) { $session = $request->getsession(); if ($request->attributes->has(securitycontextinterface::authentication_error)) { $error = $request->attributes->get( securitycontextinterface::authentification_error ); } elseif (null !== $session && $session->has(securitycontextinterface::authentication_error)) { $error = $session->get(securitycontextinterface::authentication_error); $session->remove(securitycontextinterface::authentication_error); } else { $error = null; } // last username entered user $lastemail = (null === $session) ? '' : $session->get(securitycontextinterface::last_username); return $this->render( 'theiamainbundle::security/login.html.twig', array( // last username entered user 'last_email' => $lastemail, 'error' => $error, ) ); } public function logincheckaction() { } public function logoutaction() { } public function registeraction() { $form = $this->createform(new registrationtype(), new registration()); return $this->render('theiamainbundle:security:register.html.twig', array('form' => $form->createview())); } public function createaction() { $em = $this->getdoctrine()->getentitymanager(); $form = $this->createform(new registrationtype(), new registration()); $form->handlerequest($this->getrequest()); if ($form->isvalid()) { $registration = $form->getdata(); $em->persist($registration->getuser()); $em->flush(); return $this->redirect('theiamainbundle::security/login.html.twig'); } return $this->render('theiamainbundle:security:register.html.twig', array('form' => $form->createview())); } }
here in directory form: usermaintype:
namespace theia\mainbundle\form\type; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; use symfony\component\optionsresolver\optionsresolverinterface; class usermaintype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder->add('email', 'email'); $builder->add('plainpassword', 'repeated', array( 'first_name' => 'password', 'second_name' => 'confirm', 'type' => 'password', )); } public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setdefaults(array( 'data_class' => 'theia\mainbundle\entity\usermain' )); } public function getname() { return 'user'; } }
here registrationtype
namespace theia\mainbundle\form\type; use symfony\component\form\abstracttype; use symfony\component\form\formbuilderinterface; class registrationtype extends abstracttype { public function buildform(formbuilderinterface $builder, array $options) { $builder->add('user', new usermaintype()); //$builder->add('terms', 'checkbox', array('property_path' => 'termsaccepted')); } public function getname() { return 'registration'; } }
here registration
namespace theia\mainbundle\form\model; use symfony\component\validator\constraints assert; use theia\mainbundle\entity\usermain; class registration { /** * @assert\type(type="theia\mainbundle\entity\user") */ protected $user; /* * @assert\notblank() * @assert\true() * protected $termsaccepted; */ public function setuser(user $user) { $this->user = $user; } public function getuser() { return $this->user; } /* public function gettermsaccepted() { return $this->termsaccepted; } public function settermsaccepted($termsaccepted) { $this->termsaccepted = (boolean) $termsaccepted; } */ }
thanks helps
i not quite sure think problem haven't set
'data_class' => 'theia\mainbundle\entity\registration'
in setdefaultoptions
of registrationtype, therefore registrationtype not mapped entity, therefore usermain cannot persisted since used in subform of registrationtype.
Comments
Post a Comment