Sonata Admin ManyToOne 错误:“sonata_type_collection - 映射:2" [英] Sonata Admin ManyToOne error: "sonata_type_collection - mapping : 2 "

查看:26
本文介绍了Sonata Admin ManyToOne 错误:“sonata_type_collection - 映射:2"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 News Entity 中使用 Sonata Admin Gallery 功能.这是我的代码

I want to use Sonata Admin Gallery feature into my News Entity. Here is my code

News.yml

....

manyToOne:
    gallery:
        targetEntity: Application\Sonata\MediaBundle\Entity\Gallery
        inversedBy: news_gallery
        cascade: ["persist"] 
        nullable: true

Gallery.orm.xml

....

<one-to-many field="news_gallery" 
                 target-entity="Wenweipo\NewsBundle\Entity\News"
                 mapped-by="gallery" /> 

NewsAdmin.php

protected function configureFormFields(FormMapper $formMapper) {

    $formMapper
        ->add('gallery', 'sonata_type_collection', array(
                'cascade_validation' => true,
                    ), array(
                'edit' => 'inline',
                'inline' => 'table',
                'sortable' => 'position',
                'link_parameters' => array(
                    'context' => 'images_news',
                ),
                        'admin_code'=>'sonata.media.admin.gallery_has_media'
            ))

}

现在当我运行我的管理代码时,我收到了这个错误.

Now when I run my admin code I receive this error.

INVALID MODE : s543e4bf7bc21f_gallery - type : sonata_type_collection - mapping : 2 

我做错了什么?

推荐答案

尝试建立你自己的关系,像 GalleryHasMedia

Try to make your own relation that will hold NewsHasMeda like GalleryHasMedia

News.orm.yml

........
oneToMany:
    news_has_media:
        targetEntity: Wenweipo\NewsBundle\Entity\NewsHasMedia
        mappedBy: news 
        cascade: ["persist","remove"]
        orphanRemoval: true

现在创建另一个名为NewsHasMedia.yml的实体.然后在yml文件中添加这样的代码

Now make another entity named as NewsHasMedia.yml.Then in yml file add the code like this

NewsHasMedia.orm.yml

........
fields:

    enabled:
        type: boolean
        nullable: true
    position:
        type: integer
        nullable: true
    createdAt:
        type: datetime
        column: created_at
        nullable: true
    updatedAt:
        type: datetime
        column: updated_at
        nullable: true


manyToOne:

    media:
        targetEntity: Application\Sonata\MediaBundle\Entity\Media
        cascade: ["persist"]
        joinColumn:
            name: media_id   
            referencedColumnName: id
            #nullable: true 

    news:
        targetEntity: News
        inversedBy:  news_has_media
        cascade: ["persist"]
        joinColumn:
            name: news_id   
            referencedColumnName: id

然后生成这个实体.现在在管理文件中添加这个

Then generate this entity.Now in Admin file add this one

NewsAdmin.php

->add('news_has_media', 'sonata_type_collection', array(

   'cascade_validation' => true), array(

            'edit' => 'inline',
            'inline' => 'table',
            'sortable' => 'position',
            'link_parameters' => array(
                'context' => 'images_news',
            ),

        ))

现在无需指定admin_code.希望这能解决您的问题.

now no need to specify admin_code.Hope this will solve your problem.

这篇关于Sonata Admin ManyToOne 错误:“sonata_type_collection - 映射:2"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆