简体   繁体   中英

SonataMediaBundle Usage with custom entities

I'm using Symfony2 with Sonata bundles (Sonata-Admin and Sonata-Media) to develop my CMS project.

I setup my admin classes for my custom bundle and it's working well. I need to attach images into my entities and view them in their user pages. But I have a problem with SonataMediaBundle to use in my custom entities. I'm trying to add a field to AdminController of my entity class and also create a manyToOne relationship in my entity definition.

I never got it work and generally gives "Entity was not found" Exception (Doctrine\\ORM\\EntityNotFoundException class)

I couldn't find an article except on SonataMedia Documentation and couldn't solve this problem. Is anyone know a good article to read and understand the whole concept of SonataMedia Bundle?

Some more information about my code is below:

MyEntity.orm.yml

PRJ\Bundle\PRJBundle\Entity\MyEntity:
type: entity
table: my_table
fields:
    id:
        id: true
        type: integer
        unsigned: false
        nullable: false
        generator:
            strategy: IDENTITY

# Other fields eliminated

manyToOne:
    picture:
        targetEntity: Application\Sonata\MediaBundle\Entity\Media
        joinColumns:
                name: Picture_id
                referencedColumnName: id
        orphanRemoval: false
lifecycleCallbacks: {  }

Actually this code is working on both Admin panel and user pages but I couldn't make it work on admin pages when admin user is editing the record or entering new record.

Also please correct me if I'm wrong, I'm assuming that I can generate an admin class for my custom entity that is attaching pictures. I'll add a field and define it as foreign key to media__media table in my database, and create necessary definitions on my Entities. At last I will be able to upload new pictures via SonataMediaBundle when creating new entities. Admin User will enters entity details and presses the upload button and chooses a picture, upload button works with SonataMedia bundle and uploads image to SonataMedia bundle's table also the new record will be created in my bundle's table and associated with newly created media.

I hope I could described my problem well and thank you.

If you're using custom entity classes with SonataMediaBundle, you need to specify them in config.yml :

sonata_media:
    class: 
        media: MyVendor\MediaBundle\Entity\Media
        gallery: MyVendor\MediaBundle\Entity\Gallery
        gallery_has_media: MyVendor\MediaBundle\Entity\GalleryHasMedia

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM