Sonata Admin ManyToOne error: “sonata_type_collection - mapping : 2 ”

不羁的心 提交于 2019-12-06 04:41:13

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

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',
            ),

        ))

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!