我在Symfony 2.7项目中使用FOSUserBundle
。在composer.json
文件中,要求定义为"friendsofsymfony/user-bundle" : "~2.0@dev"
。目前已安装版本/提交 45d6f40 (11/03/2015)。
使用composer update
后,安装了最新版本 7abb0ff 。在此之后,我在尝试创建新用户时遇到以下异常:
字段'usernameCanonical'未由Doctrine映射,因此无法验证其唯一性
搜索此问题的解决方案会带来处理相同异常(here和here)的旧问题。但是,我无法解决这些问题中讨论的解决方案的问题。
Issue 1565建议使用FOS\UserBundle\Entity\User as BaseUser;
代替FOS\UserBundle\Model\User as BaseUser;
。但是这个解决方案似乎对版本2.x无效。 2.x文档说你应该从FOS\UserBundle\Model\User
扩展,这是有道理的,因为没有FOS\UserBundle\Entity\...
类了。
我的用户类如下所示:
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* This class represents the User entity and extends the FOSBundle base user
* Entity class to be able to use FOSUserBundle to manage the application users.
*
* @ORM\Entity(repositoryClass="AppBundle\Entity\UserRepository")
* @ORM\Table(name="app_user") *
* @ORM\HasLifecycleCallbacks()
*/
class User extends BaseUser {
...
}
在Issue 1638中,解决方案是在Doctrine配置中使用auto_mapping
。我已经这样做了。
所以现有的解决方案都不适合我。此问题的所有现有问题都很复杂。
当然我可以简单地降级回版本/ commit 45d6f40。但是我宁愿解决问题而不是忽略它: - )
还有什么想法我能解决这个问题吗?
PS:这是我的composer show -i
输出:
doctrine/annotations v1.2.7 Docblock Annotations Parser
doctrine/cache v1.6.0 Caching library offering an object-oriented API for many cache backends
doctrine/collections v1.3.0 Collections Abstraction library
doctrine/common v2.6.1 Common Library for Doctrine projects
doctrine/dbal v2.4.5 Database Abstraction Layer
doctrine/doctrine-bundle v1.2.0 Symfony DoctrineBundle
doctrine/inflector v1.0.1 Common String Manipulations with regard to casing and singular/plural rules.
doctrine/instantiator 1.0.5 A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer v1.0.1 Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.
doctrine/orm v2.4.8 Object-Relational-Mapper for PHP
friendsofsymfony/http-cache 1.4.0 Tools to manage cache invalidation
friendsofsymfony/http-cache-bundle 1.3.4 Set path based HTTP cache headers and send invalidation requests to your HTTP cache
friendsofsymfony/rest-bundle 1.4.2 This Bundle provides various tools to rapidly develop RESTful API's with Symfony2
friendsofsymfony/user-bundle dev-master 7abb0ff Symfony FOSUserBundle
gremo/buzz-bundle v1.1.0 Symfony Bundle for using the lightweight Buzz HTTP client.
guzzle/guzzle v3.9.3 PHP HTTP client. This library is deprecated in favor of https://packagist.org/packages/guzzlehttp/guzzle
jdorn/sql-formatter v1.2.17 a PHP SQL highlighting library
jms/metadata 1.5.1 Class/method/property metadata management in PHP
jms/parser-lib 1.0.0 A library for easily creating recursive-descent parsers.
jms/serializer 1.3.1 Library for (de-)serializing data of any complexity; supports XML, JSON, and YAML.
jms/serializer-bundle 1.1.0 Allows you to easily serialize, and deserialize data of any complexity
kriswallsmith/assetic v1.3.2 Asset Management for PHP
kriswallsmith/buzz v0.15 Lightweight HTTP client
leafo/scssphp v0.6.6 scssphp is a compiler for SCSS written in PHP.
moontoast/math 1.1.0 A mathematics library, providing functionality for large numbers
paragonie/random_compat v2.0.2 PHP 5.x polyfill for random_bytes() and random_int() from PHP 7
phpcollection/phpcollection 0.5.0 General-Purpose Collection Library for PHP
phpoption/phpoption 1.5.0 Option Type for PHP
psr/log 1.0.0 Common interface for logging libraries
sensio/distribution-bundle v2.3.22 The base bundle for the Symfony Distributions
sensio/framework-extra-bundle v2.3.4 This bundle provides a way to configure your controllers with annotations
sensio/generator-bundle v2.3.5 This bundle generates code for you
swiftmailer/swiftmailer v5.4.3 Swiftmailer, free feature-rich PHP mailer
symfony/assetic-bundle v2.7.1 Integrates Assetic into Symfony2
symfony/monolog-bundle v2.8.2 Symfony MonologBundle
symfony/swiftmailer-bundle v2.3.11 Symfony SwiftmailerBundle
symfony/symfony v2.7.7 The Symfony PHP framework
tfox/mpdf-port-bundle 1.3.1 A wrapper for mPDF class which allows to use mPDF in Symfony2 projects
twig/extensions v1.0.1 Common additional features for Twig that do not directly belong in core
twig/twig v1.25.0 Twig, the flexible, fast, and secure template language for PHP
willdurand/jsonp-callback-validator v1.1.0 JSONP callback validator.
willdurand/negotiation 1.5.0 Content Negotiation tools for PHP provided as a standalone library.
答案 0 :(得分:1)
当你收到错误时,这可能会解决某些问题
字段" usernameCanonical'不是由Doctrine映射的,因此无法验证其唯一性
一个原因是使用FOS \ UserBundle \ Model \ User作为BaseUser并运行doctrine schema:update,如果你这样做,你在数据库上创建一个表,没有像username,usernameCanonical,password等几个列。
当您想要访问此功能时,您会收到该消息。
如何解决:
FOS\UserBundle\Model\User as BaseUser
至
FOS\UserBundle\Entity\User as BaseUser
php app/console doctrine:schema:update --force