【问题标题】:UniqueEntity using Doctrine 2UniqueEntity 使用 Doctrine 2
【发布时间】:2013-02-07 15:28:54
【问题描述】:

我正在使用 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);
  }

}

【问题讨论】:

标签: doctrine-orm entity zend-framework2 unique-index


【解决方案1】:

@UniqueEntity 是 symfony 验证组件的一个特殊扩展(这里写成注解)。您正在寻找的可能是您可以在DoctrineModule 中找到的验证器:https://github.com/doctrine/DoctrineModule/blob/master/docs/validator.md

还没有内置对它的支持作为注释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-14
    • 2015-02-28
    • 2013-02-19
    • 2012-08-17
    • 2012-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多