Sonata Admin ManyToOne错误:“sonata_type_collection - mapping:2”

时间:2014-10-15 10:45:10

标签: symfony sonata-admin sonata-media-bundle

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

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

}

现在,当我运行管理代码时,我收到此错误。

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

我做错了什么?

1 个答案:

答案 0 :(得分:1)

尝试建立自己的关系NewsHasMeda,如GalleryHasMedia

<强> News.orm.yml

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

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

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

然后生成此实体。现在在Admin文件中添加此实体

<强> 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。希望这可以解决您的问题。