Sonata Admin搜索功能

时间:2013-10-23 20:31:29

标签: php symfony sonata-admin

我最近更新了我的symfony2项目供应商。因此,我获得了最新的Sonata Admin Bundle版本(从2.2.5更新到2.2.6)。

我看到此版本中有一个新的搜索功能但我无法正常工作。我无法弄清楚我做错了什么。让我疯狂。

这是我的composer.json要求:

    "require": {
    "php": ">=5.3.3",
    "symfony/symfony": "2.3.*",
    "doctrine/orm": ">=2.2.3,<2.4-dev",
    "doctrine/doctrine-bundle": "1.2.*",
    "twig/extensions": "1.0.*",
    "symfony/assetic-bundle": "2.3.*",
    "symfony/swiftmailer-bundle": "2.3.*",
    "symfony/monolog-bundle": "2.3.*",
    "sensio/distribution-bundle": "2.3.*",
    "sensio/framework-extra-bundle": "2.3.*",
    "sensio/generator-bundle": "2.3.*",
    "incenteev/composer-parameter-handler": "~2.0",

    "jms/security-extra-bundle": "1.5.*",
    "doctrine/data-fixtures": "1.0.*",
    "doctrine/doctrine-fixtures-bundle": "dev-master",
    "doctrine/migrations": "dev-master",
    "doctrine/doctrine-migrations-bundle": "dev-master",
    "stof/doctrine-extensions-bundle": "1.1.*",
    "friendsofsymfony/user-bundle" : "1.3.*",
    "knplabs/knp-menu-bundle": "1.1.*",
    "knplabs/gaufrette": "dev-master",
    "knplabs/knp-paginator-bundle": "2.3.*",

    "sonata-project/easy-extends-bundle" : "2.1.*",
    "sonata-project/cache-bundle": "2.1.*",
    "sonata-project/jquery-bundle": "1.8.*",
    "sonata-project/exporter": "1.3.*",
    "sonata-project/block-bundle": "2.2.*",
    "sonata-project/user-bundle": "2.2.*@dev",
    "sonata-project/admin-bundle": "2.2.*",
    "sonata-project/doctrine-orm-admin-bundle": "2.2.*",
    "sonata-project/doctrine-extensions": "1.0.0",
    "sonata-project/google-authenticator": "1.0.0",
    "sonata-project/intl-bundle": "2.2.*@dev",
    "sonata-project/media-bundle": "2.2.*",
    "sonata-project/notification-bundle": "2.2.*",
    "sonata-project/formatter-bundle": "2.3.*",

    "hwi/oauth-bundle": "0.3.*@dev",
    "elao/web-profiler-extra-bundle": "2.3.*@dev",
    "liip/functional-test-bundle": "dev-master",
    "guzzle/guzzle": "v3.4.1",
    "jms/serializer-bundle": "0.12.*@dev",
    "friendsofsymfony/rest-bundle": "0.12.0",
    "friendsofsymfony/comment-bundle": "2.0.*@dev",
    "whiteoctober/breadcrumbs-bundle": "dev-master",
    "igorw/file-serve-bundle": "1.0.*",
    "zendframework/zendpdf": "2.0.*"
},

在更新后,我有一个搜索功能抱怨一个不存在的块:

  

在渲染模板期间抛出异常(“The   块类型sonata.admin.block.search_result不存在“)   SonataAdminBundle:核心:第48行的search.html.twig

我通过在config.yml

中声明阻止来更正此问题
sonata_block:
    default_contexts: [cms]
    blocks:
        sonata.admin.block.admin_list:
            contexts:   [admin]

        sonata.admin.block.search_result:
            contexts:   [admin]

        sonata.block.service.text:
        sonata.block.service.rss:

但现在,它抱怨未设置请求:

  

在渲染模板期间抛出异常(“The   请求对象尚未设置为“)   SonataAdminBundle:核心:第48行的search.html.twig

我真的不明白我在这里缺少什么。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:19)

在奏鸣曲块配置中添加块:

sonata_block:
    default_contexts: [cms]
    blocks:
        sonata.admin.block.admin_list: #default sonata admin block
            contexts: [admin]
        sonata.admin.block.search_result:
            contexts: [admin]

答案 1 :(得分:7)

我得到了同样的错误。 这可能是因为您在管理员中使用了请求。

$this->getRequest()->get('context', 'default')

如果您尝试获取请求,搜索不会设置请求,管理员会触发异常。

在我的管理员中,我正在使用一个函数来访问请求参数并避免异常。

public function getRequestParameterOrNull( $key, $default = null ){
    try{
        return $this->getRequest()->get($key, $default);
    }catch(\Exception $e){
        return null;
    }
}

答案 2 :(得分:1)

在下面找到完整配置:

<强> composer.json

"require": {
    "php": ">=5.4",
    "symfony/symfony": "2.2.*",
    "doctrine/orm": "~2.2,>=2.2.3",
    "doctrine/doctrine-bundle": "1.2.*",
    "twig/extensions": "dev-master",
    "symfony/assetic-bundle": "2.3.*",
    "symfony/swiftmailer-bundle": "2.2.*",
    "symfony/monolog-bundle": "2.2.*",
    "sensio/distribution-bundle": "2.2.*",
    "sensio/framework-extra-bundle": "2.2.*",
    "sensio/generator-bundle": "v2.3.3",
    "jms/security-extra-bundle": "1.4.*",
    "jms/di-extra-bundle": "1.3.*",
    "friendsofsymfony/user-bundle": "*",
    "friendsofsymfony/elastica-bundle": "2.1.*@dev",
    "sonata-project/cache-bundle": "dev-master",
    "sonata-project/block-bundle": "dev-master",
    "sonata-project/jquery-bundle": "1.8.*",
    "sonata-project/exporter" : "1.*",
    "knplabs/knp-menu":"1.1.*",
    "knplabs/knp-menu-bundle":"1.1.*",
    "sonata-project/admin-bundle": "dev-master#da071a913b86619fdd35449713a53ffd4cde1f61",
    "sonata-project/doctrine-orm-admin-bundle": "dev-master",
    "sonata-project/intl-bundle": "dev-master",
    "sonata-project/user-bundle": "dev-master",
    "sonata-project/media-bundle": "dev-master",
    "sonata-project/notification-bundle": "dev-master",
    "sonata-project/easy-extends-bundle": "dev-master",
    "stfalcon/tinymce-bundle": "v0.3.0",
    "stof/doctrine-extensions-bundle": "~1.1@dev",
    "a2lix/translation-form-bundle": "0.*@dev",
    "genemu/form-bundle": "2.1.*",
    "friendsofsymfony/jsrouting-bundle": "~1.1",
    "ibrows/newsletter-bundle": "dev-master",
    "elao/web-profiler-extra-bundle" : "dev-master",
    "leafo/lessphp": "dev-master",
    "raulfraile/ladybug-bundle": "~1.0",
    "jms/serializer-bundle": ">=0.11,<0.13-dev",
    "friendsofsymfony/comment-bundle": "v2.0.5",
    "pasinter/admanager-bundle": "dev-master",
    "liip/imagine-bundle": "dev-master"
},

我刚刚强制对sonata管理包进行特定提交,只是为了确保我们使用相同的版本。我建议您在完成工作版本后从配置中删除#da071a913b86619fdd35449713a53ffd4cde1f61字符串。

<强> config.yml

sonata_user:
    security_acl:     false
    class:
        user:         Application\Sonata\UserBundle\Entity\User
        group:        Application\Sonata\UserBundle\Entity\Group

    profile:  # Profile Form (firstname, lastname, etc ...)
        form:
            type:               sonata_user_profile
            handler:            sonata.user.profile.form.handler.default
            name:               sonata_user_profile_form
            validation_groups:  [Profile]

sonata_block:
    default_contexts: [cms]
    blocks:
        sonata.admin.block.admin_list:
            contexts:   [admin]
        sonata.admin.block.search_result:
            contexts:   [admin]

        sonata.block.service.text:
        sonata.block.service.action:
        sonata.block.service.rss:

sonata_admin:
    title:      Admin Panel
    title_logo: /bundles/sonataadmin/logo_title.png
    templates:
        # default global templates
        layout:  AcmeAdminBundle:Default:standard_layout.html.twig
        ajax:    SonataAdminBundle::ajax_layout.html.twig

        # default actions templates, should extend a global templates
        list:    SonataAdminBundle:CRUD:list.html.twig
        show:    SonataAdminBundle:CRUD:show.html.twig
        edit:    SonataAdminBundle:CRUD:edit.html.twig

        # search
        search:              SonataAdminBundle:Core:search.html.twig
        search_result_block: SonataAdminBundle:Block:block_search_result.html.twig
    dashboard:
        blocks:
            # display a dashboard block
            - { position: left, type: sonata.admin.block.admin_list }

sonata_doctrine_orm_admin:
    # default value is null, so doctrine uses the value defined in the configuration
    entity_manager: ~

    templates:
        form:
            - SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig
        filter:
            - SonataDoctrineORMAdminBundle:Form:filter_admin_fields.html.twig
        types:
            list:
                array:      SonataAdminBundle:CRUD:list_array.html.twig
                boolean:    SonataAdminBundle:CRUD:list_boolean.html.twig
                date:       SonataAdminBundle:CRUD:list_date.html.twig
                time:       SonataAdminBundle:CRUD:list_time.html.twig
                datetime:   SonataAdminBundle:CRUD:list_datetime.html.twig
                text:       SonataAdminBundle:CRUD:base_list_field.html.twig
                trans:      SonataAdminBundle:CRUD:list_trans.html.twig
                string:     SonataAdminBundle:CRUD:base_list_field.html.twig
                smallint:   SonataAdminBundle:CRUD:base_list_field.html.twig
                bigint:     SonataAdminBundle:CRUD:base_list_field.html.twig
                integer:    SonataAdminBundle:CRUD:base_list_field.html.twig
                decimal:    SonataAdminBundle:CRUD:base_list_field.html.twig
                identifier: SonataAdminBundle:CRUD:base_list_field.html.twig

            show:
                array:      SonataAdminBundle:CRUD:show_array.html.twig
                boolean:    SonataAdminBundle:CRUD:show_boolean.html.twig
                date:       SonataAdminBundle:CRUD:show_date.html.twig
                time:       SonataAdminBundle:CRUD:show_time.html.twig
                datetime:   SonataAdminBundle:CRUD:show_datetime.html.twig
                text:       SonataAdminBundle:CRUD:base_show_field.html.twig
                trans:      SonataAdminBundle:CRUD:show_trans.html.twig
                string:     SonataAdminBundle:CRUD:base_show_field.html.twig
                smallint:   SonataAdminBundle:CRUD:base_show_field.html.twig
                bigint:     SonataAdminBundle:CRUD:base_show_field.html.twig
                integer:    SonataAdminBundle:CRUD:base_show_field.html.twig
                decimal:    SonataAdminBundle:CRUD:base_show_field.html.twig

官方文档:http://sonata-project.org/bundles/admin/master/doc/reference/search.html