使用 MongoDB 配置 Sonata Media Bundle 的问题

Posted

技术标签:

【中文标题】使用 MongoDB 配置 Sonata Media Bundle 的问题【英文标题】:Issue Configuring Sonata Media Bundle with MongoDB 【发布时间】:2014-09-06 09:27:06 【问题描述】:

我正在尝试使用 MongoDB 配置 SonataMediaBundle,但在运行 composer 或清除缓存后出现此异常:

[ErrorException] 可捕获的致命错误:传递给 Sonata\DoctrineMongoDBAdminBundle\Model\ModelManager::__construct() 的参数 1 必须是 Symfony\Bridge\Doctrine\ManagerRegistry 的实例,给定的 Doctrine\ODM\MongoDB\DocumentManager 实例,在第 22 行的 ../vendor/sonata-project/media-bundle/Admin/Manager/DoctrineMongoDBManager.php 中调用并在 /Users/interrobang/Sites/aag/vendor/sonata-project/doctrine-mongodb-admin-bundle 中定义/Sonata/DoctrineMongoDBAdminBundle/Model/ModelManager.php 第 39 行

这是我的 composer.json


    "name": "symfony/framework-standard-edition",
    "license": "MIT",
    "type": "project",
    "description": "The \"Symfony Standard Edition\" distribution",
    "autoload": 
        "psr-0":  "Antenna": "src/" 
    ,
    "require": 
        "php": ">=5.3.3",
        "symfony/symfony": "~2.4",
        "doctrine/orm": "~2.2,>=2.2.3",
        "doctrine/doctrine-bundle": "~1.2",
        "doctrine/mongodb-odm": "1.0.*@beta",
        "doctrine/mongodb-odm-bundle": "3.0.*@beta",
        "twig/extensions": "~1.0",
        "symfony/assetic-bundle": "~2.3",
        "symfony/swiftmailer-bundle": "~2.3",
        "symfony/monolog-bundle": "~2.4",
        "sensio/distribution-bundle": "~2.3",
        "sensio/framework-extra-bundle": "~3.0",
        "sensio/generator-bundle": "~2.3",
        "incenteev/composer-parameter-handler": "~2.0",
        "symfony/config": "~2.0",
        "symfony/yaml": "~2.2",
        "symfony/translation": "v2.4.5",
        "friendsofsymfony/user-bundle": "~2.0@dev",
        "jackalope/jackalope-mongodb": "dev-master@dev",
        "sonata-project/admin-bundle": "dev-master@dev",
        "sonata-project/doctrine-mongodb-admin-bundle": "dev-master",
        "sonata-project/media-bundle": "dev-master@dev",
        "jackalope/jackalope-doctrine-dbal": "dev-master@dev"
    ,
    "scripts": 
        "post-install-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
        ],
        "post-update-cmd": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
        ]
    ,
    "config": 
        "bin-dir": "bin"
    ,
    "extra": 
        "symfony-app-dir": "app",
        "symfony-web-dir": "web",
        "incenteev-parameters": 
            "file": "app/config/parameters.yml"
        ,
        "branch-alias": 
            "dev-master": "2.4-dev"
        
    

还有我的 config.yml

imports:
    -  resource: parameters.yml 
    -  resource: security.yml 

framework:
    #esi:             ~
    #translator:       fallback: "en" 
    translator: ~
    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:  "en"
    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%"
    #form:
    #    resources:
    #        - 'CmfMediaBundle:Form:fields.html.twig'

# Assetic Configuration
assetic:
    debug:          "%kernel.debug%"
    use_controller: false
    bundles:        [ AntennaArtGalleryBundle ]
    #java: /usr/bin/java
    filters:
        cs-s-rewrite: ~
        #closure:
        #    jar: "%kernel.root_dir%/Resources/java/compiler.jar"
        #yui_css:
        #    jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"

sonata_block:
    default_contexts: [cms]
    blocks:
        # Enable the SonataAdminBundle block
        sonata.admin.block.admin_list:
            contexts:   [admin]
        # Your other blocks

# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:      type: memory 

fos_user:
    db_driver: mongodb
    firewall_name: main
    user_class: Antenna\ArtGalleryBundle\Document\User
    registration:
        form:
            type: aag_admin_user_registration

# Mongo DB Doctrine Configuration
doctrine_mongodb:
    connections:
        default:
            server: mongodb://localhost:27017
            options: 
    default_database: test_database
    document_managers:
        default:
            mappings:
                #ApplicationSonataUserBundle: ~
                #SonataUserBundle: ~
                AntennaArtGalleryBundle: ~
                FOSUserBundle: ~
            #metadata_cache_driver:
            #    type: memcache
            #    class: Doctrine\Common\Cache\MemcacheCache
            #    host: localhost
            #    port: 11211
            #    instance_class: Memcache

sonata_media:
    default_context: default
    db_driver: doctrine_mongodb
    contexts:
        default:
            providers:
                - sonata.media.provider.dailymotion
                - sonata.media.provider.youtube
                - sonata.media.provider.image
                - sonata.media.provider.file

            formats:
                small:  width: 100 , quality: 70
                big:    width: 500 , quality: 70

    cdn:
        server:
            path: /uploads/media

    filesystem:
        local:
            directory:  %kernel.root_dir%/../web/uploads/media
            create:     false

有什么想法吗?

【问题讨论】:

【参考方案1】:

您必须覆盖sonata.media.admin.media.manager 服务才能注入doctrine_mongodb 而不是doctrine_mongodb.odm.document_manager

所以你必须在你的services.yml 文件中添加这个:

sonata.media.admin.media.manager:
    class: Sonata\MediaBundle\Admin\Manager\DoctrineMongoDBManager
    arguments: [@doctrine_mongodb, @sonata.media.manager.media]

这是一个修复,但仅适用于 orm (https://github.com/sonata-project/SonataMediaBundle/commit/9903f901db703ac441cbea18a1a8be1dc8c9c7e8)。

等待https://github.com/sonata-project/SonataMediaBundle/pull/611 被合并。

【讨论】:

以上是关于使用 MongoDB 配置 Sonata Media Bundle 的问题的主要内容,如果未能解决你的问题,请参考以下文章

Sonata mongodb admin bundle filter by reference

symfony2:奏鸣曲 userbundle + fos userbundle + mongodb => 错误“sonata.user.mongodb.user_manager”不存在

Sonata Admin MongoDB DataGrid 过滤器

Sonata admin 基于 ODM 的子管理员在基于 ORM 的管理员上

如何在 Sonata Admin 中正确配置“sonata_type_collection”字段

显示配置文件 FOS UserBundle + Sonata UserBundle