独特的使用原则2

独特的使用原则2

本文介绍了独特的使用原则2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Zend Framework 2,并创建了一个用户实体。现在我试图使用户名字段唯一。然而,以下错误即将出现。

  [语义错误]类User \Entity\中的注释@UniqueEntity用户从未导入你可能忘了为这个注释添加一个使用语句? 

我已经添加了此代码进行唯一性检查

  @UniqueEntity(email)

我可以看到它是Symfony中使用的一种方法。如何使用Zend Framework 2?



这是我使用的实体

 <?php 

命名空间User\Entity;

使用Doctrine\ORM\Mapping作为ORM,
Zend\Form\Annotation;

/ **
*用户实体。
*
* @ 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(类型= 整数);
* @ ORM\GeneratedValue(strategy =AUTO)
*
* @ Annotation\Required(false)
* /
protected $ id;

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

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

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

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

/ **
* Magic setter保存受保护属性。
*
* @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->邮政编码,
);
}

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

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

}


解决方案

是一个symfony验证组件的特殊扩展(在这里写为注释),

@UniqueEntity 您正在寻找的可能是您可以在 DoctrineModule 中找到的验证器:



支持它作为注释尚未内置。


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 in DoctrineModule: https://github.com/doctrine/DoctrineModule/blob/master/docs/validator.md

Support for it as annotation is not yet built-in.

这篇关于独特的使用原则2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:01