Bonjour,
Je veux faire upload fichier mais j'ai un probléme .. move est fonctionne c'est à dire déplacement du fichier est fonctionne mais path en base de données est vide

code entity media:

<?php

        namespace AppBundle\Entity;

        use Doctrine\ORM\Mapping as ORM;
        use Symfony\Component\validator\Constraints as Assert;
        use Symfony\Component\HttpFoundation\File\UploadedFile;

        /**
         * Media
         *
         * @ORM\Table(name="medias")
         * @ORM\Entity(repositoryClass="AppBundle\Entity\Repository\MediaEntityRepository")
         * @ORM\HasLifecycleCallbacks
         */
        class Media
        {
            /**
             * @var integer
             *
             * @ORM\Column(name="id", type="integer")
             * @ORM\Id
             * @ORM\GeneratedValue(strategy="AUTO")
             */
            private $id;

             /**
             * @var \DateTime
             *
             * @ORM\COlumn(name="updated_at",type="datetime", nullable=true)
             */
            private $updateAt;

            /**
             * @ORM\PostLoad()
             */
            public function postLoad()
            {
                $this->updateAt = new \DateTime();
            }

            /**
             * @ORM\Column(type="string",length=255, nullable=true)
             */
            public $path;

            public $file;

              public function getUploadRootDir()
            {
                return __dir__.'/../../../web/bundles/app/img';
            }

            public function getAbsolutePath()
            {
                return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
            }

            public function getAssetPath()
            {
                return 'uploads/'.$this->path;
            }

            /*
             *@ORM\PrepPersist()
             * @ORM\PreUpdate()
             */
            public function preUpload()
            {
                $this->tempFile = $this->getAbsolutePath();
                $this->oldFile = $this->getPath();
                $this->updateAt = new \DateTime();

                if (null !== $this->file)
                    $this->path = sha1(uniqid(mt_rand(),true)).'.'.$this->file->guessExtension();
            }

            /**
             * @ORM\PostPersist()
             * @ORM\PostUpdate()
             */
              public function upload()
        {
            // the file property can be empty if the field is not required
            if (null === $this->getFile()) {
                return;
            }

            // use the original file name here but you should
            // sanitize it at least to avoid any security issues

            // move takes the target directory and then the
            // target filename to move to
            $this->getFile()->move(
                $this->getUploadRootDir(),
                $this->getFile()->getClientOriginalName()
            );

            // set the path property to the filename where you've saved the file
            $this->path = $this->getFile()->getClientOriginalName();

            // clean up the file property as you won't need it anymore
            $this->file = null;
        }

            /**
             * @ORM\PreRemove()
             */
            public function preRemoveUpload()
            {
                $this->tempFile = $this->getAbsolutePath();
            }

            /**
             * @ORM\PostRemove()
             */
            public function removeUpload()
            {
                if (file_exists($this->tempFile)) unlink($this->tempFile);
            }

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

            public function setPath($path)
            {
                $this->path = $path;

                return $this;
            }

            /**
             * Get path
             *
             * @return string
             */
            public function getPath()
            {
                return $this->path;
            }

            /**
             * Get name
             *
             * @return \string
             */

             public function getName()
            {
                return $this->name;
            }

            /**
             * Set updateAt
             *
             * @param \DateTime $updateAt
             * @return Media
             */
            public function setUpdateAt($updateAt)
            {
                $this->updateAt = $updateAt;

                return $this;
            }

            /**
             * Get updateAt
             *
             * @return \DateTime
             */
            public function getUpdateAt()
            {
                return $this->updateAt;
            }

            /**
             * Set name
             *
             * @param string $name
             * @return Media
             */
            public function setName($name)
            {
                $this->name = $name;

                return $this;
            }

            /**
             * Sets file.
             *
             * @param UploadedFile $file
             */
                public function setFile(UploadedFile $file = null)
            {
                $this->file = $file;
            }

            /**
             * Get file.
             *
             * @return UploadedFile
             */
            public function getFile()
            {
                return $this->file;
            }

        }

Code entity User:


        /**
         *@ORM\OneToOne(targetEntity="Media",cascade={"persist"})
         */
        private $image;

         /**
         * Set image
         *
         * @param \AppBundle\Entity\Media $image
         * @return User
         */
        public function setImage(\AppBundle\Entity\Media $image = null)
        {
            $this->image = $image;

            return $this;
        }

        /**
         * Get image
         *
         * @return \AppBundle\Entity\Media 
         */
        public function getImage()
        {
            return $this->image;
        }

code mediaType:

<?php

    namespace AppBundle\Form;

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\FormBuilderInterface;
    use Symfony\Component\OptionsResolver\OptionsResolver;
    use Symfony\Component\Form\Extension\Core\Type\FileType;

    /**
     * MediaType.   
     */
    class MediaType extends AbstractType
    {
        /**
         * {@inheritdoc}
         * @param FormBuilderInterface $builder
         * @param array $options
         */
        public function buildForm(FormBuilderInterface $builder, array $options)
        {
            $builder->add('file',FileType::class,array('required'=>false));
        }

        /**
         * {@inheritdoc}
         * @param OptionsResolver $resolver
         */
        public function configureOptions(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'AppBundle\Entity\Media'
            ));
        }

        /**
         * {@inheritdoc}
         */
        public function getBlockPrefix()
        {
            return 'appbundle_media';
        }

    }

code user type:

<?php

    namespace AppBundle\Form;

    use Symfony\Component\Form\AbstractType;
    use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
    use Symfony\Component\Intl\Intl;
    use Symfony\Component\Form\FormBuilderInterface;
    use AppBundle\Form\MediaType;

    /**
     *Class DefaultController.
     */
    class CondidatType extends AbstractType
    {
        /**
         * function buildForm when I add many attributs added by entity User.
         *
         * @param FormBuilderInterface $builder
         * @param array                $options
         */
        public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options)
        {
            $countries = Intl::getRegionBundle()->getCountryNames();

            $builder
                    ->add('civility', ChoiceType::class, array('choices' => array('Mr' => 'MR', 'Mrs' => 'MRS'), 'expanded' => true, 'multiple' => false))
                    ->add('image',MediaType::class)
                    ->add('first_name', null)
                    ->add('last_name', null)
                    ->add('country', ChoiceType::class, array('choices' => array_flip($countries), 'label' => 'Country'))
                    ->add('phone', null)
                    ->add('birthday',null);
        }

        /**
         * function get Parent Form.
         *
         * @return string
         */
        public function getParent()
        {
            return 'FOS\UserBundle\Form\Type\RegistrationFormType';
        }

        /**
         * name for this form.
         *
         * @return string
         */
        public function getBlockPrefix()
        {
            return 'condidatuser_registration';
        }

        /**
         * name for this form.
         *
         * @return string
         */
        public function getName()
        {
            return $this->getBlockPrefix();
        }

        /**
         * function setDefaultOptions.
         *
         *@param OptionsResolverInterface $resolver
         */
        public function setDefaultOptions(OptionsResolverInterface $resolver)
        {
            $resolver->setDefaults(array(
                'data_class' => 'AppBundle\Entity\User',
            ));
        }
    }

donc par exemple quand il j'upload image .. déplacement fonctionne avec move mais path en base de données est vide

1 réponse


med001
Auteur

Personne m'aider s'il vous plait