gpt4 book ai didi

php - Symfony2 UniqueEntity 约束 SQL 错误而不是消息

转载 作者:搜寻专家 更新时间:2023-10-31 21:29:05 25 4
gpt4 key购买 nike

我坚持使用 Symfony2 创建用户注册表单。我试图在我的 User 类的 email 属性上定义一个 Unique 约束。

Acme\APPBundle\Entity\User.php

namespace Acme\APPBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
* @ORM\Entity
* @ORM\Entity(repositoryClass="Acme\APPBundle\Entity\UserRepository")
* @ORM\Table("users")
* @UniqueEntity(
* fields={"email"},
* message="email already used"
* )
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\Column(type="string", length=255, unique=true)
* @Assert\NotBlank()
* @Assert\Email()
*/
protected $email;


[...]
}

Acme\APPBundle\Form\Type\UserType.php

namespace Acme\APPBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('email', 'email');
$builder->add('password', 'repeated', array(
'first_name' => 'password',
'second_name' => 'confirm',
'type' => 'password',
));
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\APPBundle\Entity\User',
'cascade_validation' => true,
));
}

public function getName()
{
return 'user';
}
}

我已经在文档之后添加了约束,但我仍然遇到如下异常:

An exception occured while executing 'INSERT INTO users ( ... )'
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry

看起来我在注释中定义的 message 值被忽略并且表单验证被绕过,因为它应该在尝试将行插入数据库之前失败。

你能告诉我我做错了什么吗?


编辑:

按照 Matteo 'Ingannatore' G. 的建议,我注意到我的表格没有得到正确验证。我忘了提到我使用了一个扩展用户表单的注册类。我已经按照 Symfony Cookbook 中的解释编写了我的代码.

因此我有:

Acme\APPBundle\Form\Model\Registration.php

namespace Acme\APPBundle\Form\Model;

use Symfony\Component\Validator\Constraints as Assert;

use Acme\APPBundle\Entity\User;

class Registration
{
/**
* @Assert\Type(type="Acme\APPBundle\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;
}
}

Acme\APPBundle\Form\Type\RegistrationType.php

namespace Acme\APPBundle\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 UserType());
$builder->add('terms', 'checkbox', array('property_path' => 'termsAccepted'));
}

public function getName()
{
return 'registration';
}
}

Acme\APPBundle\Controller\AccountController.php

namespace Acme\APPBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

use Acme\AccountBundle\Form\Type\RegistrationType;
use Acme\AccountBundle\Form\Model\Registration;

class AccountController extends Controller
{
public function registerAction()
{
$form = $this->createForm(new RegistrationType(), new Registration());

return $this->render('AcmeAPPBundle:Account: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()) { // FIXME !!
$registration = $form->getData();

$em->persist($registration->getUser());
$em->flush();

return $this->redirect($this->generateUrl('home'));
}

return $this->render('AcmeAPPBundle:Account:register.html.twig', array('form' => $form->createView()));
}

}

我想我收到的错误可能是由于 Registration 表单已验证,但其中的 User 表单未提交验证。我错了吗 ?我怎样才能简单地改变这种行为?我看到有一个 cascade_validation 选项,但它在这里似乎没用。我认为 Symfony Cookbook 提供创建用户提供者和创建注册表单的指南很奇怪,但没有解释如何完成这些工作。

最佳答案

我终于找到了真正的问题所在。验证仅在 RegistrationType 实例上处理,而不在 UserType 中处理。

为了确保验证还检查用户的约束,我将以下代码添加到我的 RegistrationType 类中:

public function setDefaultOptions(Options ResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\APPBundle\Form\Model\Registration',
'cascade_validation' => true,
));
}

改变一切的是 cascade_validation 选项,该选项必须为此类设置为 true,而此选项是在 UserType 类上设置的CookBook 示例。

此外,不要忘记:

use Symfony\Component\OptionResolver\OptionsResolverInterface

在您定义 setDefaultOptions 的文件中。

关于php - Symfony2 UniqueEntity 约束 SQL 错误而不是消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32189743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com