我正在使用 Zend Framework 2,并且我创建了一个用户实体。现在我正在尝试使用户名字段唯一。但是,出现以下错误。
[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?
我已添加此代码以进行唯一性检查
@UniqueEntity("email")
我可以看到这是 Symfony 中使用的一种方法。我如何将它用于 Zend Framework 2?
这是我正在使用的实体
<?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);
}
}