问题
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
What I m doing wrong?
回答1:
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.
来源:https://stackoverflow.com/questions/26380592/sonata-admin-manytoone-error-sonata-type-collection-mapping-2