user3588551
user3588551

Reputation: 83

How to upload image with SonataMediaBundle

I started with SonataMediaBundle, and I have some problems when I want to upload an image (The documentation is not quite clear unfortunately)

I proceeded as :

I want to upload image in create News Form, so :

In News Entity :

/**
 * @ORM\ManyToOne(targetEntity="\Application\Sonata\MediaBundle\Entity\Media", cascade={"all"})
 * @ORM\JoinColumn(nullable=false)
 */
protected $imageNews;

In NewsType, I add the field in buildForm :

    public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        ->add('title')
        ->add('summary', 'text', array(
        'required' => false
        ))
        ->add('body', 'textarea', array('attr' => array(
          'class' => 'ckeditor',
        )))
        ->add('imageNews', 'sonata_media_type', array(
            'provider' => 'sonata.media.provider.image',
            'context'  => 'news'
        ));
    //    ->add('categories', 'taxonomy')
        ;
}

My file config.yml

Doctrine :
orm:
    auto_generate_proxy_classes: "%kernel.debug%"
    auto_mapping: true


# SonataMediaBundle
sonata_media:
    default_context: default
    db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr
    contexts:
        default:  # the default context is mandatory
            providers:
                - sonata.media.provider.dailymotion
                - sonata.media.provider.youtube
                - sonata.media.provider.image
                - sonata.media.provider.file

            formats:
                small: { width: 100 , quality: 70}
                big:   { width: 500 , quality: 70}
        news:
            providers:
                - sonata.media.provider.youtube
                - sonata.media.provider.image

            formats:
                small: { width: 150 , quality: 95}
                big:   { width: 500 , quality: 90}

    cdn:
        server:
            path: /uploads/media # http://media.sonata-project.org/

    filesystem:
        local:
            directory:  %kernel.root_dir%/../web/uploads/media
            create:     false
    providers:
        image:
            resizer: sonata.media.resizer.square

I think I missed something, can you tell me what is missing please; I managed to display the uploaded but when I validate the form I see that imageNews object is Null (var_dump)

Thank you !

Upvotes: 1

Views: 1718

Answers (1)

PayteR
PayteR

Reputation: 1757

Same issue here, problem was solved with adding this form template

twig:
    debug:            %kernel.debug%
    strict_variables: %kernel.debug%

    form:
        resources:
            # other files
            - 'SonataMediaBundle:Form:media_widgets.html.twig'

http://sonata-project.org/bundles/media/2-2/doc/reference/form.html

in that template must be something thats the key for make

Upvotes: 2

Related Questions