如何使用SonataMediaBundle上传图像

时间:2014-07-04 09:16:50

标签: symfony sonata-admin symfony-sonata

我从SonataMediaBundle开始,当我想上传图片时遇到一些问题(不幸的是文档不太清楚)

我继续:

我想在创建新闻表单中上传图片,所以:

在新闻实体中:

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

在NewsType中,我在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')
        ;
}

我的文件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

我想我错过了什么,你能告诉我遗失的东西吗? 我设法显示上传但是当我验证表单时,我看到imageNews对象是Null(var_dump)

谢谢!

1 个答案:

答案 0 :(得分:2)

此处的问题相同,添加此表单模板解决了问题

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

该模板中的

必须是make的关键词