php - 在 symfony2 中使用 sonata-bundle,如何更加便捷地管理数据信息?

 wang-zhiwen 发布于 2022-11-15 14:53

我目前有3张表goodscategory and brand,在sonata后台中,我希望可以把类别名称和品牌名称全部显示在货品列表,下图有详细的信息:

从上图可以看出,iphone6plus的类别是mobile,这里我在yml文件中定义了many-to-one的关系,这里显示是正确的,但是brand.name很明显错了,iphone6plus的品牌应该是apple,这里我有疑问,我我在 goods.yml 文件中定义了两次many-to-one关系,第二次定义的是goodsbrand表,并没有正确显示。

我想更便捷地管理货品信息,在同一页面,浏览商品类别和品牌,在编辑页面同理,我应该如何实现呢?下面是我的代码块:

brand.orm.yml

Application\Sonata\MallBundle\Entity\Brand:
    type: entity
    table: brand
    ...
oneToMany:
    goods:
        targetEntity: Goods
        mappedBy: brand
lifecycleCallbacks: {  }

goods.orm.yml

Application\Sonata\MallBundle\Entity\Goods:
type: entity
table: goods
repositoryClass: Application\Sonata\MallBundle\Repository\GoodsRepository
...
manyToOne:
    category:
        targetEntity: Category
        inverseBy: goods
        joinColumn:
            name: category_id
            referencedColumnName: id
manyToOne:
    brand:
        targetEntity: Brand
        inverseBy: goods
        joinColumn:
            name: brand_id
            referencedColumnName: id
lifecycleCallbacks: {  }

category.orm.yml

Application\Sonata\MallBundle\Entity\Category:
type: entity
table: category
repositoryClass: Application\Sonata\MallBundle\Repository\CategoryRepository
...
oneToMany:
    goods:
        targetEntity: Goods
        mappedBy: category
lifecycleCallbacks: {  }

您的建议,对我来说是莫大的帮助。

1 个回答
  • 方案:

    你需要把关注点放在GoodsAdmin(通常位于AppBundle\Admin命名空间下)这个类中。

    实现你想要的结果,你需要针对这个类,做如下修改(注意中文注释)。

    class GoodsAdmin extends AbstractAdmin
    {
    
        // ...
    
        /**
         * @param ListMapper $listMapper
         */
        protected function configureListFields(ListMapper $listMapper) // 配置表格
        {
            $listMapper
                // ...
                ->add('category.name') // 显示类别名称
                ->add('brand.name') // 显示品牌名称
                // ...
            ;
        }
        
        /**
         * @param FormMapper $formMapper
         */
        protected function configureFormFields(FormMapper $formMapper) // 配置表单
        {
            $formMapper
                // ...
                ->add('category', 'sonata_type_model', [ // 显示类别选择控件
                    'class' => 'AppBundle\Entity\Category', // 关联实体
                    'property' => 'name', // 显示的属性
                ])
                ->add('brand', 'sonata_type_model', [ // 显示品牌选择控件
                    'class' => 'AppBundle\Entity\Brand',
                    'property' => 'name',
                ])
                // ...
            ;
        }
    
        // ...
        
    }

    要点:

    • 第一个方法中,通过小数点dot notation)的形式访问关联实体associated entity)的属性 (property),用来在表格中显示。

    • 第二个方法中,通过使用SonataAdminBundlesonata_type_model表单类型(form type),在当前表单中生成关联实体的选择控件,同时还可以直接在当前表单中新建(通过Ajax)。

    • 如果熟悉Symfony Form组件的用法,该类中的各个方法的配置用起来就会比较自然。建议看下前者的用法。

    总结:

    SonataAdminBundle官方文档质量一般,更新缓慢,对开发者容易造成阻碍,但是,建议还是仔细啃一下官方文档,然后开发起来就容易多了。文档链接:ADMIN BUNDLE

    2022-11-15 15:15 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有