是否可以在Symfony2中为“用户名”提供重复的条目

时间:2017-09-05 02:10:05

标签: php symfony doctrine-orm

在我们的Symfony2应用程序中,我们有一个名为sfGuardUser的类,它扩展自FOS\UserBundle\Model\User(FOSUserBundle包的一部分)。所以它继承了该User类的$ username字段,当我尝试插入重复的用户名时会抛出错误。我要做的是检查用户是否确认了他/她的电话号码和电子邮件地址,然后再抛出该错误。

例如,当某人使用用户名“Anna”注册但未确认其电子邮件和电话号码时。其他人应该可以使用用户名“Anna”注册。我该如何实现这种行为?

我试图做attribute override但没有成功。这是代码:

use FOS\UserBundle\Model\User as BaseUser;

/**
 * @ORM\Entity(repositoryClass="sfGuard\Bundle\sfGuardBundle\Entity\sfGuardUserRepository")
 * @ORM\Table(name="sf_guard_user")
 * @ORM\AttributeOverrides({
 *     @ORM\AttributeOverride(name="email",
 *          column=@ORM\Column(
 *              name     = "email_address",
 *              type     = "string",
 *              length   = 250,
 *              unique   = true
 *          )
 *      ),
 *      @ORM\AttributeOverride(name="username",
 *          column=@ORM\Column(
 *              name     = "username",
 *              type     = "string",
 *              length   = 250,
 *              unique   = false
 *          )
 *      ),
 *      @ORM\AttributeOverride(name="enabled",
 *          column=@ORM\Column(
 *              name     = "is_active",
 *              type     = "boolean",
 *              nullable = true,
 *              options  = {
 *                  "default" : 1
 *              }
 *          )
 *      )
 * })
 * @ORM\HasLifecycleCallbacks
 */
class sfGuardUser extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="first_name", type="string", length=255, nullable=true)
     * @Assert\NotBlank()
     * @Assert\Length(
     *     min = 2,
     *     minMessage = "Too short."
     * )
     */
    protected $firstName;
}

1 个答案:

答案 0 :(得分:0)

这里是回答

<?php
namespace LoginBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
/**
 * Contacts
 *
 * @ORM\Table(name="Contacts")
 * @ORM\Entity(repositoryClass="LoginBundle\Repository\ContactsRepository")
 * @ORM\AttributeOverrides({
 *     @ORM\AttributeOverride(name="username",
 *          column=@ORM\Column(
 *              nullable = true
 *          )
 *      ),
 *     @ORM\AttributeOverride(name="usernameCanonical",
 *          column=@ORM\Column(
 *              name = "username_canonical",
 *              nullable = true
 *          )
 *      ),
 *     @ORM\AttributeOverride(name="email",
 *          column=@ORM\Column(
 *              nullable = true
 *          )
 *      ),
 *     @ORM\AttributeOverride(name="emailCanonical",
 *          column=@ORM\Column(
 *              name = "email_canonical",
 *              nullable = true
 *          )
 *      )
 * })
 */
class Contacts extends BaseUser
{ 
   /**
    * @var int
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
   protected $id;
   /**
    * @var int
    *
    * @ORM\Column(name="Type", type="integer",nullable=true)
    */
   protected $type;
   /**
    * Get id
    *
    * @return integer
    */
   public function getId()
   {
    return $this->id;
   }
  /**
   * Set type
   *
   * @param integer $type
   * @return Contacts
   */
  public function setType($type)
   {
     $this->type = $type;
     return $this;
   }
 /**
  * Get type
  *
  * @return integer
  */
 public function getType()
  {
    return $this->type;
  }
}

请参阅此帖attribute overrides for username fosuserbundle