symfony - 唯一实体不起作用

标签 symfony doctrine-orm

@UniqueEntity 不适合我。在 Controller 中,我使用 $form->isValid() 和表单传递,但它不应该。相反,我自己定义了消息,但我收到了 MySQL 错误:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'media30' for key 'UNIQ_115E494BF47645AE'

我有以下表格:

    class BIPType extends AbstractType{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name');
        $builder->add('url');
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'AppBundle\Entity\Bip',
        );
    }
}

接下来,覆盖 FOSUser UserType 表单进行注册,其中包括 BIPType:

class UserType extends AbstractType{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $biptype = new BIPType();
        $builder
            ->add('bip', $biptype, array(
            'data_class'=>'AppBundle\Entity\Bip'))
            ->add('nazwisko')
            ->add('imie')
            ;
    }

    public function getParent()
    {
        return 'FOS\UserBundle\Form\Type\RegistrationFormType';
        // Or for Symfony < 2.8
        // return 'fos_user_registration';
    }

    public function getBlockPrefix()
    {
        return 'app_user_registration';
    }

    // For Symfony 2.x
    public function getName()
    {
        return $this->getBlockPrefix();
    }
}

和BIP实体

    use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;


/**
 * @ORM\Entity
 * @UniqueEntity(fields="url", message="URL is already in use")
 * @ORM\Table(name="bips")
 */
class Bip
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM\Column(name="url", type="string", unique=true)
     */
    protected $url;

    /**
     * @ORM\Column(type="string")
     * @Assert\Length(
     *      min = "3",
     *      max = "25",
     *      minMessage = "Nazwa BIPu musi mieć conajmniej 3 znaki.",
     *      maxMessage = "Nazwa BIPu może mieć conajwyżej 25 znaki."
     *)
     */
    protected $name;


    /**
     * @Assert\Image(
     *     minWidth = 80,
     *     maxWidth = 200,
     *     minHeight = 80,
     *     maxHeight = 200
     * )
     */
    protected $file;

    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
    private $temp;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $path;

    /**
     * @ORM\Column(type="boolean", nullable=false)
     */
    private $public;

最佳答案

如果您使用 FOSUserBundlefos_user.registration.form.factory 服务创建表单,则默认情况下,Registration验证表单时使用验证组。将名为 Registration 的验证组添加到您的 name 属性中。

/**
 * @ORM\Entity
 * @ORM\Table(name="bips")
 * @UniqueEntity(fields="url", message="URL is already in use", groups={"Registration", "Profile"})
 */
class Bip
{

然后添加

use Symfony\Component\OptionsResolver\OptionsResolver;

public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'validation_groups' => array('Registration'),
    ));
}

到您的BIPType类。

关于symfony - 唯一实体不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36221481/

相关文章:

symfony - 将数据从 Controller 传递到 symfony2 类型

php - Symfony2.5 中的 FOSUserBundle : how to erase the data table User with an existing

mysql - 使用 findBy 访问实体内的 ArrayCollection

Symfony + JMSSerializer 抛出 500 -handleCircularReference

symfony - "Service xxx has a dependency on a non-existent service"这似乎不是真的?

json - Symfony2 - 实体 findAll 在 API 上返回大量响应

php - Doctrine 2.1 - 日期时间列默认值

doctrine-orm - Zend Framework 2 Doctrine 快速入门

php - 学说 2 : reattaching entities through value objects

php - Symfony2,doctrine自定义DQL函数