UniqueEntity不起作用

时间:2016-03-25 14:05:05

标签: symfony doctrine-orm

@UniqueEntity对我不起作用。在控制器中我使用$ form-> isValid()和表单传递,但它不应该。而是我自己定义的消息,我得到了MySQL错误:

  

SQLSTATE [23000]:完整性约束违规:1062重复条目' media30'关键词' 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;

2 个答案:

答案 0 :(得分:0)

那是因为

def html_weapon_info(conn, %{"weapon_name" => weapon_name}) do
  weapon = Repo.get_by(Weapon, name: weapon_name)
  ships = Weapon |> Weapon.get_ships(weapon_name) |> Repo.all
  render(conn, "weapon_show.html", weapon: weapon, ships: ships)
end

是数据库列的定义,但不是验证规则 这就是为什么在将数据插入数据库时​​会出现错误的原因,但在传递$ form-&gt; isValid()方法时则不会出现错误

希望,这会对你有所帮助

Unique Constraints in Doctrine 2, Symfony 2

答案 1 :(得分:0)

如果您使用fos_user.registration.form.factory FOSUserBundle服务创建表单,则默认情况下,在验证表单时会使用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班。