UniqueEntity using Doctrine 2

I am using Zend Framework 2 and I have created a User Entity. Now I am trying to make the username field unique. However the following error appears.

[Semantical Error] The annotation "@UniqueEntity" in class User\Entity\User was never imported. Did you maybe forget to add a "use" statement for this annotation?

      

I added this code to check for uniqueness

@UniqueEntity("email")

      

I can see that this is the method used in Symfony. How can I use it for Zend Framework 2?

This is the object I am using

<?php

namespace User\Entity;

use Doctrine\ORM\Mapping as ORM,
    Zend\Form\Annotation;

/**
 * A user entity.
 *
 * @ORM\Entity
 * @ORM\Table(name="users")
 * @UniqueEntity("email")

 * @property int $id
 * @property string $username
 * @property string $email
 * @property string $password
 * 
 * @Annotation\Name("User")
 */
class User {

  /**
   * @ORM\Id
   * @ORM\Column(type="integer");
   * @ORM\GeneratedValue(strategy="AUTO")
   * 
   * @Annotation\Required(false)
   */
  protected $id;

  /**
   * @ORM\Column(type="string")
   * 
   * @Annotation\Attributes({"type":"text"})
   * @Annotation\Options({"label":"Username:"})
   * @Annotation\Filter({"name":"StringTrim"})
   * @Annotation\Filter({"name":"StripTags"})
   */
  protected $username;

  /**
   * @ORM\Column(type="string")
   * 
   * @Annotation\Attributes({"type":"text" })
   * @Annotation\Options({"label":"Email:"})
   * @Annotation\Filter({"name":"StringTrim"})
   * @Annotation\Filter({"name":"StripTags"})
   */
  protected $email;

  /**
   * @ORM\Column(type="string")
   * 
   * @Annotation\Attributes({"type":"text" })
   * @Annotation\Options({"label":"Password:"})
   * @Annotation\Filter({"name":"StringTrim"})
   * @Annotation\Filter({"name":"StripTags"})
   */
  protected $password;    

  public function __get($property) {
    return $this->$property;
  }

  /**
   * Magic setter to save protected properties.
   *
   * @param string $property
   * @param mixed $value
   */
  public function __set($property, $value) {
    $this->$property = $value;
  }

  public function getArrayCopy() {
    return array(
        'username' => $this->username,
        'email' => $this->email,
        'surname' => $this->surname,
        'first_name' => $this->first_name,
        'company' => $this->company,
        'postcode' => $this->postcode,
    );
  }

  public function populate($data) {
    $this->username = isset($data['username']) ? $data['username'] : $this->username;
  }

  public function setDate($property, $value){
    $this->$property = new \DateTime($value);
  }

}

      

+3


source to share


1 answer


@UniqueEntity

is a specific extension of the symfony validator component (written as an annotation here). What you are looking for is probably the validators you can find at DoctrineModule

: https://github.com/doctrine/DoctrineModule/blob/master/docs/validator.md



Support for it as annotations is not built in yet.

+3


source







All Articles