3

最近,我开始使用 Symfony2。现在我想向我的站点添加一个用户管理引擎。

但我面临一个问题。这就是我正在做的事情:

  1. 在创建/安装一个基本的 Symfony2 项目方面:

    $ composer create-project symfony/framework-standard-edition path/ "2.5.*"
    $ mv path/* ./
    $ rm -r path/
    
  2. 好的,Symfony 2.5.5 就这么多。接下来,下载 FOSUserBundle 并创建一个自定义包:

    $ composer require friendsofsymfony/user-bundle '~2.0@dev'
    $ php app/console generate:bundle --namespace=Meiblorn/CoreBundle --format=yml
    
  3. 在 Meiblorn\CoreBundle\Framework\Domain 命名空间中创建 User 类

    /**
     * User: Meiblorn
     * Date: 15/10/14
     * Time: 20:17
     */
    
    namespace Meiblorn\CoreBundle\Framework\Domain;
    
    use FOS\UserBundle\Model\User as FOSUserBundleUser;
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity
     * @ORM\Table(
     *      name = "users"
     * )
     */
    class User extends FOSUserBundleUser {
    
        /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        protected $id;
    
        public function __construct() {
            parent::__construct();
            // your own logic
        }
    
    }
    
    ?>
    
  4. 配置 security.yml 和 config.yml。最后,我得到了这个:

    应用内核.php

    $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
    
            new FOS\UserBundle\FOSUserBundle(),
    
            new Meiblorn\CoreBundle\MeiblornCoreBundle(),
    );
    

    配置.yml

    imports:
        - { resource: parameters.yml }
        - { resource: security.yml }
    
    framework:
        #esi:             ~
        translator:      { fallback: "%locale%" }
        secret:          "%secret%"
        router:
            resource: "%kernel.root_dir%/config/routing.yml"
            strict_requirements: ~
        form:            ~
        csrf_protection: ~
        validation:      { enable_annotations: true }
        templating:
            engines: ['twig']
            #assets_version: SomeVersionScheme
        default_locale:  "%locale%"
        trusted_hosts:   ~
        trusted_proxies: ~
        session:
            # handler_id set to null will use default session handler from php.ini
            handler_id:  ~
        fragments:       ~
        http_method_override: true
    
    # Twig Configuration
    twig:
        debug:            "%kernel.debug%"
        strict_variables: "%kernel.debug%"
    
    # Assetic Configuration
    assetic:
        debug:          "%kernel.debug%"
        use_controller: false
        bundles:        [ ]
        #java: /usr/bin/java
        filters:
            cssrewrite: ~
            #closure:
            #    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
            #yui_css:
            #    jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"
    
    # Doctrine Configuration
    doctrine:
        dbal:
            driver:   "%database_driver%"
            host:     "%database_host%"
            port:     "%database_port%"
            dbname:   "%database_name%"
            user:     "%database_user%"
            password: "%database_password%"
            charset:  UTF8
        orm:
            auto_generate_proxy_classes: "%kernel.debug%"
            auto_mapping: true
    
    # Swiftmailer Configuration
    swiftmailer:
        transport: "%mailer_transport%"
        host:      "%mailer_host%"
        username:  "%mailer_user%"
        password:  "%mailer_password%"
        spool:     { type: memory }
    
    fos_user:
        db_driver: orm
        firewall_name: prod
        user_class: Meiblorn\CoreBundle\Framework\Domain\User
    

    安全.yml

    security:
            encoders:
                FOS\UserBundle\Model\UserInterface: sha512
    
            role_hierarchy:
                ROLE_ADMIN:       ROLE_USER
                ROLE_SUPER_ADMIN: ROLE_ADMIN
    
            providers:
                fos_userbundle:
                    id: fos_user.user_provider.username
    
            firewalls:
                prod:
                    pattern: ^/
                    form_login:
                        provider: fos_userbundle
                        csrf_provider: form.csrf_provider
                    logout:       true
                    anonymous:    true
                dev:
                    pattern: ^/(_(profiler|wdt)|css|images|js)/
                    security: false
    
            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 }
    

这就是问题

In browser:  http://localhost/test.meiblorn.com/web/app_dev.php/

MappingException: The class 'Meiblorn\CoreBundle\Framework\Domain\User' was not found in the chain configured namespaces FOS\UserBundle\Model
in /Library/WebServer/Documents/test.meiblorn.com/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php line 37
at MappingException::classNotFoundInNamespaces('Meiblorn\CoreBundle\Framework\Domain\User', array('FOS\UserBundle\Model')) in /Library/WebServer/Documents/test.meiblorn.com/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/MappingDriverChain.php line 113
at MappingDriverChain->loadMetadataForClass('Meiblorn\CoreBundle\Framework\Domain\User', object(ClassMetadata)) in /Library/WebServer/Documents/test.meiblorn.com/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php line 117
at ClassMetadataFactory->doLoadMetadata(object(ClassMetadata), object(ClassMetadata), false, array()) in /Library/WebServer/Documents/test.meiblorn.com/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php line 318

此外,Doctrine 在调用doctrine:schema:update 时不会为此映射创建表

请帮我解决这个异常


更新!怎么修

我的命名空间的最终配置

orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        auto_mapping: false
        mappings:
            FOSUserBundle: ~
            MeiblornCoreBundle:
                type: annotation
                dir: %kernel.root_dir%/../src/Meiblorn/CoreBundle/Framework/Entity
                prefix: Meiblorn\CoreBundle\Framework\Entity
              # alias: MyModels
              # is_bundle: true
4

1 回答 1

3

首先你需要在你的 composer.js 中配置 psr-4 自动加载,例如

"autoload": {
    "psr-4": {
      "Meiblorn\\CoreBundle\\": "src/Meiblorn/CoreBundle/"
    }
},

然后调用composer dumpautoload

其次,我相信 Doctrine 期望实体存在于一个文件夹Entity/中,所以尝试将你的模型移动src/Meiblorn/CoreBundle/Framework/Domain/User.phpsrc/Meiblorn/CoreBundle/Entity/User.php或者我如何更改 symfony 2 教义映射器以使用我的自定义目录而不是捆绑包下的我的实体目录

于 2014-10-16T14:48:39.487 回答