首页 文章

无法识别的字段:symfony2 FosUserbundle的usernameCanonical

提问于
浏览
0

我的FOSUserBundle有问题 . 一个已知问题:当我尝试登录时,'无法识别的字段:用户名为symfony2 FosUserbundle'

在架构更新上我收到此错误:

Duplicate definition of column 'username' on entity 'Acme\ProjectBundle\Entity\User' in a field or discriminator column mapping.

只有在我在config.yml中添加'FOSUserBundle:〜'到doctrine映射的设置时才会出现此错误

我已经尝试了很多解决方案,但我没有解决我的问题:/请帮助我 .

我跟随了FOS的团队:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md

在一切运作完美之前......

我有Symfony 2.1.9

我的config.yml:

doctrine:
dbal:
    default_connection: default
    connections:
        default:
            driver:   "%database_driver%"
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
        service:
            driver:   "%database_driver2%"
            host:     "%database_host2%"
            port:     "%database_port2%"
            dbname:   "%database_name2%"
            user:     "%database_user2%"
            password: "%database_password2%"
            charset:  UTF8
orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    #auto_mapping: true
    default_entity_manager: default
    entity_managers:
        default:
            metadata_cache_driver: apc
            result_cache_driver: apc
            query_cache_driver: apc
            connection: default
            mappings:
                FOSUserBundle: ~
                AcmeProjectBundle: {type: yml, dir: Resources/config/doctrine/ } #also tried wit '~'
fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: Acme\ProjectBundle\Entity\User

我的Acme \ ProjectBundle \ Entity \ User.php:

namespace Acme\ProjectBundle\Entity;

 use FOS\UserBundle\Entity\User as BaseUser;
 use Doctrine\ORM\Mapping as ORM;
 use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
 use Symfony\Component\Validator\Constraints as Assert;
 use Symfony\Component\Security\Core\User\UserInterface;


 class User extends BaseUser implements UserInterface, \Serializable
 {
      const TYPE_ADMIN        = 0;
      const TYPE_USER         = 2;
      const TYPE_ARTIST       = 3;

/**
 * @var string $salt
 */
protected $salt;

/**
 * @var boolean $is_active
 */
private $is_active;
protected $id;


private $name;


protected $username;

protected $email;

/**
 * @var tinyint $type
 */
private $type;


protected $password;


private $description;


/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 * @return User
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}

/**
 * Set type
 *
 * @param integer $type
 * @return User
 */
public function setType($type)
{
    $this->type = $type;

    return $this;
}

/**
 * Get type
 *
 * @return integer 
 */
public function getType()
{
    return $this->type;
}

/**
 * Set description
 *
 * @param string $description
 * @return User
 */
public function setDescription($description)
{
    $this->description = $description;

    return $this;
}

/**
 * Get description
 *
 * @return string 
 */
public function getDescription()
{
    return $this->description;
}

/**
 * Set username
 *
 * @param string $username
 * @return User
 */
public function setUsername($username)
{
    $this->username = $username;

    return $this;
}

/**
 * Get username
 *
 * @return string 
 */
public function getUsername()
{
    return $this->username;
}

/**
 * Set password
 *
 * @param string $password
 * @return User
 */
public function setPassword($password)
{
    $this->password = $password;

    return $this;
}

/**
 * Get password
 *
 * @return string 
 */
public function getPassword()
{
    return $this->password;
}

/**
 * Set email
 *
 * @param string $email
 * @return User
 */
public function setEmail($email)
{
    $this->email = $email;

    return $this;
}

/**
 * Get email
 *
 * @return string 
 */
public function getEmail()
{
    return $this->email;
}




public function isPasswordName()
{
    return ($this->name != $this->password);
}

public function isPassUsername()
{
    return ($this->password != $this->username);
}
/**
 * @var \DateTime $date
 */
private $date;


/**
 * Set date
 *
 * @param \DateTime $date
 * @return User
 */
public function setDate($date)
{
    $this->date = $date;

    return $this;
}

/**
 * Get date
 *
 * @return \DateTime 
 */
public function getDate()
{
    return $this->date;
}


private $updateDate;


/**
 * Set updateDate
 *
 * @param \DateTime $updateDate
 * @return User
 */
public function setUpdateDate($updateDate)
{
    $this->updateDate = $updateDate;

    return $this;
}

/**
 * Get updateDate
 *
 * @return \DateTime 
 */
public function getUpdateDate()
{
    return $this->updateDate;
}



public function setIsActive($value)
{
    $this->is_active = $value;

    return $this;
}

public function gettIsActive()
{
    return $this->is_active;

    return $this;
}


/**
 * Set salt
 *
 * @param string $salt
 * @return User
 */
public function setSalt($salt)
{
    $this->salt = $salt;

    return $this;
}

/**
 * Get salt
 *
 * @return string 
 */
public function getSalt()
{
    return $this->salt;
}


/**
 * @inheritDoc
 */
public function eraseCredentials()
{
}


public function __construct()
{
parent::__construct();
    $this->isActive = true;
    $this->salt = md5(uniqid(null, true));
}


public function getRoles()
{
    switch ($this->getType())
    {
            case 0:
                    return array('ROLE_ADMIN');
                    break;
            case 1:
            case 2:
            case 3:
                    return array('ROLE_USER');
                    break;

    }


}

/**
 * @see \Serializable::serialize()
 */
public function serialize()
{
    return serialize(array(
                    $this->id,
    ));
}

/**
 * @see \Serializable::unserialize()
 */
public function unserialize($serialized)
{
    list (
                    $this->id,
    ) = unserialize($serialized);
}
/**
 * @var integer $first_login
 */
private $first_login;


/**
 * Get is_active
 *
 * @return boolean 
 */
public function getIsActive()
{
    return $this->is_active;
}

/**
 * Set first_login
 *
 * @param integer $firstLogin
 * @return User
 */
public function setFirstLogin($firstLogin)
{
    $this->first_login = $firstLogin;

    return $this;
}

/**
 * Get first_login
 *
 * @return integer 
 */
public function getFirstLogin()
{
    return $this->first_login;
}
/**
 * @var \Doctrine\Common\Collections\ArrayCollection
 */
private $userPoints;





/**
 * @var integer $privacy
 */
private $privacy;


/**
 * Set privacy
 *
 * @param integer $privacy
 * @return User
 */
public function setPrivacy($privacy)
{
    $this->privacy = $privacy;

    return $this;
}

/**
 * Get privacy
 *
 * @return integer 
 */
public function getPrivacy()
{
    return $this->privacy;
}
/**
 * @var integer
 */
private $enable;


/**
 * Set enable
 *
 * @param integer $enable
 * @return User
 */
public function setEnable($enable)
{
    $this->enable = $enable;

    return $this;
}

/**
 * Get enable
 *
 * @return integer 
 */
public function getEnable()
{
    return $this->enable;
}
}

我的security.yml

security:
firewalls:
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            login_path: /login
            use_forward: false
            check_path: /login_check
            csrf_provider: form.csrf_provider
        logout:       true
        anonymous:    true
providers:
    fos_userbundle:
        id: fos_user.user_provider.username

encoders:
    FOS\UserBundle\Model\UserInterface: sha512

role_hierarchy:
    ROLE_ADMIN:       ROLE_USER
    ROLE_SUPER_ADMIN: ROLE_ADMIN

access_control:
    - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { path: ^/admin/, role: ROLE_ADMIN }

5 回答

  • 2

    事实上,我想,你应该在config.yml上有一些代码

    fos_user:  
        db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
        firewall_name: main  
        user_class: Acme\ProjectBundle\Entity\User
    

    你应该没有必要添加某事 . 到FOSUser的 orm 设置 .

  • 1

    看来你的FOSUserBundle版本不好......我有一个1.3 . *的问题,我解决了将其更改为版本“~2.0@dev” .

    你可以看一下你的“fos_user”表;如果它只包含一个“id”字段,则您的用户实体不会扩展正确的“FOS \ Entity \ User”对象...尝试在“composer.json”中升级所需的捆绑版本,然后重建您的表(在完整的HARD重建之下 - 数据丢失):

    php app/console doctrine:schema:drop --force
    php app/console doctrine:schema:create
    

    如果您的“fos_user”表包含所有必填字段,那么您就是好的 .

  • 3

    查看有关创建使用类的文档:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md

    您的User类不应该重复基类的所有属性(用户名等) . 它应该只有name和id等新属性 .

    虽然你没有显示你的学说映射文件,但我猜你可能重复的一切都很好吗?仅映射新属性 .

  • 0
    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        #auto_mapping: true
        default_entity_manager: default
        entity_managers:
           default:            
              // that's for APCu or APC
              metadata_cache_driver: apc
              result_cache_driver: apc
              query_cache_driver: apc
    

    在dev env APC禁用,这就是你得到这个错误的原因 . 您必须为dev注释它或启用cacheClassLoader

  • 0

    我遇到了麻烦并应用了PieroWbmstr的建议,开始使用2.0而不是1.3.6 ...在config.yml中将doctrine.orm.auto_mapping设置为true

    一旦我进行了composer.json版本的切换和升级,我的学说:schema:update立即将当前数据库实例中的新字段识别为缺失并应用它们 .

    FOS / UserBundle中较新版本的用户类似乎没有任何重大更改会迫使映射很好地发挥作用 . 有没有人知道这两个版本的区别是什么?或者更直接地说,为什么旧版本不知道doctrine认识到它的xml映射(提示:在我的两个FOS / UserBundle版本中,我将本地自定义包设置为使用注释) .

    谢谢

相关问题