简体   繁体   中英

Could not load type “sonata_media_type”: class does not exist

Hello I use SonataMedia Bundle for the 1st time, but I can not reach the link with my entitie Post; it gives me # "Could not load type "sonata_media_type": class does not exist. "# here is my code: Config.php

    sonata_media:
        db_driver: doctrine_orm 
        default_context: default
        contexts:
            Post:
                providers:
                    - sonata.media.provider.image

                formats:
                    small: { width: 100 , quality: 70}
                    big:   { width: 1680 , quality: 100}



**PostType:**


public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('media', 'sonata_media_type', array(
                     'provider' => 'sonata.media.provider.image',
                     'context'  => 'post'
            ));
    }
**My Class Post**



class Post
    {
        /**
         * @var int
         *
         * @ORM\Column(name="id", type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;


        /**
         * @var \Application\Sonata\MediaBundle\Entity\Media
         * @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"persist"})
         */
        protected $media;


        /**
         * Get id
         *
         * @return int
         */
        public function getId()
        {
            return $this->id;
        }

        /**
         * @param MediaInterface $media
         */
        public function setMedia(MediaInterface $media)
        {
            $this->media = $media;
        }

        /**
         * @return MediaInterface
         */
        public function getMedia()
        {
            return $this->media;
        }

    }

I followed this documentation to do it : [ https://sonata-project.org/blog/2013/10/11/mediabundle-mediatype-improved][1] Please How can I link my entity post correctely to entity media???

You should use the fully qualified class name MediaType::class instead, like you do in any Symfony form since v.2.8:

**PostType:**


    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('media', MediaType::class, array(
                     'provider' => 'sonata.media.provider.image',
                     'context'  => 'post'
            ));
    }

您可能忘记在formBuilder中添加此use类

use Sonata\MediaBundle\Form\Type\MediaType;

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