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 is coming up.
[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 have added this code for uniqueness check
@UniqueEntity("email")
I can see that it is a method used in Symfony. How can I use it for Zend Framework 2?
This is the Entity 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);
}
}
@UniqueEntity
is a particular extension of the symfony validation component (written as annotation here). What you are looking for is probably the validators you can find inDoctrineModule
: https://github.com/doctrine/DoctrineModule/blob/master/docs/validator.mdSupport for it as annotation is not yet built-in.