我有点问题。我按照本教程创建了一个寄存器公式,但它并没有持久化实体。我不明白为什么。它不会造成任何错误,它只是......不会冲洗。
这是教程: http://symfony.com/fr/doc/2.5/cookbook/doctrine/registration_form.html
这是实体:
namespace theia\mainBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as 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;
/**
* Get 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;
}
/**
* Get 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;
}
/**
* Get 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;
}
/**
* Get roles
*
* @return array
*/
public function getRoles()
{
return $this->roles;
}
public function __construct()
{
$this->roles = [ 'ROLE_USER' ];
}
}
这是我的"安全控制器":
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 by the user
$lastEmail = (null === $session) ? '' : $session->get(SecurityContextInterface::LAST_USERNAME);
return $this->render(
'theiamainBundle::security/login.html.twig',
array(
// last username entered by the 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()));
}
}
以下是目录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';
}
}
这是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';
}
}
这是注册
namespace theia\mainBundle\Form\Model;
use Symfony\Component\Validator\Constraints as 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;
}
*/
}
感谢您的帮助
答案 0 :(得分:0)
我不太确定,但我认为问题在于你没有设置
setDefaultOptions
在RegistrationType的{{1}}中,因此RegistrationType不会映射到实体,因此UserMain不能保留,因为它在RegistrationType的子表单中使用。