Skip to content

Convert application from single to multiple images upload #698

Open
@adigoun2005

Description

@adigoun2005

I am happy to find this bundle about multiple file upload in symfony. But my problem is this one: since i did already manually single file upload i do want to implement this new feature without affecting existing file tables.
Please, find in attachment my entity media and related entity.

<?php

namespace ORUKA\ORUKABundle\Entity;

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert;
/**
 * media
 *
 * @ORM\Table("media")
 * @ORM\Entity(repositoryClass="ORUKA\ORUKABundle\Repository\mediaRepository")
 * @ORM\HasLifecycleCallbacks
 */
class media
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\Column(name="hauteur", type="integer")
     *
     */
    public $hauteur;

    /**
     * @var integer
     *
     * @ORM\Column(name="largeur", type="integer")
     *
     * @Assert\NotNull()
     */

    public $largeur;

    /**
     * @var \DateTime
     * 
     * @ORM\COlumn(name="updated_at",type="datetime", nullable=true) 
     */
    public $updateAt;
    
    /**
     * @ORM\PostLoad()
     */
    public function postLoad()
    {
        $this->updateAt = new \DateTime();
    }
    
    /**
     * @ORM\Column(type="string",length=255) 

     */
    public $alt;
    
    /**
     * @ORM\Column(type="string",length=255) 
     * @Assert\NotNull(message="Votre Image n'est pas valide")
     * @Assert\NotBlank(message="Votre Image n'est pas valide")
     */   
    public $path;
    
    
    public  $file;
    
    public function getUploadRootDir()
    {
        return __dir__.'/../../../../web/PHOTOS';
    }
    
    public function getAbsolutePath()
    {
        return null === $this->path ? null : $this->getUploadRootDir().'/'.$this->path;
    }
    
    public function getAssetPath()
    {
        return 'PHOTOS/'.$this->path;
    }
    
    /**
     * @ORM\PrePersist()
     * @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();
        }
        
         if(isset($this->file)){
            $data = getimagesize($this->file); 
            $largeur = $data[0];
            $hauteur = $data[1];
            $this->setHauteur($hauteur);
            $this->setLargeur($largeur);
            if($largeur<300 || $hauteur<300 || $largeur>2000 || $hauteur>2000){
                $this->path= "";
                $this->file= NULL;
                $this->alt = "Votre image est trop grande ou trop petite veuillez choisir une autre image s.v.p";
            }
        }
    }
    
    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate() 
     */
    public function upload()
    {
        if (null !== $this->file) {
            $this->file->move($this->getUploadRootDir(),$this->path);
            unset($this->file);
            
            if ($this->oldFile != null) unlink($this->tempFile);
        }
    }
    
    /**
     * @ORM\PreRemove() 
     */
    public function preRemoveUpload()
    {
        $this->tempFile = $this->getAbsolutePath();
    }
    
    /**
     * @ORM\PostRemove() 
     */
    public function removeUpload()
    {
        if ($this->path != "") {
            unlink($this->tempFile);
        }
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
        /**
     * Set path
     *
     * @param string $path
     *
     * @return media
     */
    public function setPath($path)
    {
        $this->path = $path;

        return $this;
    }
    public function getPath()
    {
        return $this->path;
    }
       /**
     * Set alt
     *
     * @param string $alt
     *
     * @return media
     */
    public function setAlt($alt)
    {
        $this->alt = $alt;

        return $this;
    }

    public function getName()
    {
        var_dump($this->name);
        return $this->name;
    }
     /**
     * Set largeur
     *
     * @param integer $largeur
     *
     * @return media
     */
    public function setLargeur($largeur)
    {
        $this->largeur = $largeur;

        return $this;
    }

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

    /**
     * Set hauteur
     *
     * @param integer $hauteur
     *
     * @return media
     */
    public function setHauteur($hauteur)
    {
        $this->hauteur = $hauteur;

        return $this;
    }

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

    /**
     * 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;
    }

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

This uses media class in one-one

<?php

namespace ORUKA\ORUKABundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
 * services
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="ORUKA\ORUKABundle\Repository\servicesRepository")
 * @ORM\HasLifecycleCallbacks
 */
class services
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToOne(targetEntity="ORUKA\ORUKABundle\Entity\media", cascade={"persist","remove"})
     */
    private $media;
   

    /**
     * @ORM\ManyToOne(targetEntity="ORUKA\UserBundle\Entity\User", inversedBy="services")
     */
    private $User;  
    
    /**
     * @var string
     *
     * @ORM\Column(name="nom", type="string", length=255)
     * @Assert\Length(minMessage="Le nom du service est trop court",maxMessage="Le nom du service est trop long", min="3",max="30")
     */
    private $nom;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="text")
     * @Assert\Length(minMessage="La description est trop courte",maxMessage="La description est trop longue", min="5",max="150")
     */
    private $description;
    
    /**
     * @var string
     *
     * @ORM\Column(name="contact", type="string")
     * @Assert\Range(
     *      min = 0000001,
     *      max = 99999999,
     *      invalidMessage = "Veuillez entrer un numero de telephone valide S.V.P",
     *      minMessage = "Votre numero de telephone doit faire  8 chiffres",
     *      maxMessage = "Votre numero de telephone doit faire  8 chiffres"
     * )
     */
    private $contact;
    
    /**
     * @var string
     *
     * @ORM\Column(name="email", type="string", length=255,nullable=true, nullable=true)
     * @Assert\Email(message="Entrez une adresse email existante S.V.P", checkMX=true)
     */
    private $email;
    
    
    /**
     * @var string
     *
     * @ORM\Column(name="commune", type="string", length=255,nullable=true)
     * @Assert\NotNull(message="Choisissez une commune S.V.P") 
     */
    private $commune;
    
    /**
     * @var string
     *
     * @ORM\Column(name="website", type="string", length=255,nullable=true)
     */
    private $website;
    
    /**
     * @var string
     *
     * @ORM\Column(name="pays", type="string", length=255)
     * @Assert\NotNull(message="Choisissez un pays S.V.P")
     */
    private $pays;
    
    /**
     * @var string
     *
     * @ORM\Column(name="ville", type="string", length=255)
     * @Assert\NotNull(message="choisissez une ville S.V.P")
     */
    private $ville;
    
    /**
     * @var string
     *
     * @ORM\Column(name="complement", type="text")
     * @Assert\Length(
     *      min = 5,
     *      max = 150,
     *      minMessage = "Preciser votre adresse S.V.P",
     *      maxMessage = "Precisez votre adresse en moins de 150 caracteres"
     * )
     */
    private $complement;
    
    


    /**
    * @ORM\ManyToOne(targetEntity="ORUKA\ORUKABundle\Entity\categories", inversedBy="services")
    * @ORM\JoinColumn(name="categories_id", referencedColumnName="id")
    */       
    private $categories;


    /**
     * @var \DateTime
     * 
     * @ORM\Column(name="updated_at",type="datetime", nullable=true) 
     */
    private $updateAt;
    
    /**
     * @ORM\PostLoad()
     */
    public function postLoad()
    {
        $this->updateAt = new \DateTime();
    }
    

    

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

  

    /**
     * Set nom
     *
     * @param string $nom
     *
     * @return services
     */
    public function setNom($nom)
    {
        $this->nom = ucfirst($nom);

        return $this;
    }

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

    /**
     * Set description
     *
     * @param string $description
     *
     * @return services
     */
    public function setDescription($description)
    {
        $this->description = ucfirst($description);

        return $this;
    }

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

    /**
     * Set contact
     *
     * @param integer $contact
     *
     * @return services
     */
    public function setContact($contact)
    {
        $this->contact = $contact;

        return $this;
    }

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

    /**
     * Set email
     *
     * @param string $email
     *
     * @return services
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

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

    /**
     * Set website
     *
     * @param string $website
     *
     * @return services
     */
    public function setWebsite($website)
    {
        $this->website = $website;

        return $this;
    }

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

    /**
     * Set pays
     *
     * @param string $pays
     *
     * @return services
     */
    public function setPays($pays)
    {
        $this->pays = $pays;

        return $this;
    }

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

    /**
     * Set ville
     *
     * @param string $ville
     *
     * @return services
     */
    public function setVille($ville)
    {
        $this->ville = $ville;

        return $this;
    }

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

    /**
     * Set complement
     *
     * @param string $complement
     *
     * @return services
     */
    public function setComplement($complement)
    {
        $this->complement = ucfirst($complement);

        return $this;
    }

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

    /**
     * Set media
     *
     * @param \ORUKA\ORUKABundle\Entity\media $media
     *
     * @return services
     */
    public function setMedia(\ORUKA\ORUKABundle\Entity\media $media = null)
    {
        $this->media = $media;

        return $this;
    }

    /**
     * Get media
     *
     * @return \ORUKA\ORUKABundle\Entity\media
     */
    public function getMedia()
    {
        return $this->media;
    }

    /**
     * Set user
     *
     * @param \ORUKA\UserBundle\Entity\User $user
     *
     * @return services
     */
    public function setUser(\ORUKA\UserBundle\Entity\User $user = null)
    {
        $this->User = $user;

        return $this;
    }

    /**
     * Get user
     *
     * @return \ORUKA\UserBundle\Entity\User
     */
    public function getUser()
    {
        return $this->User;
    }

    /**
     * Set categories
     *
     * @param \ORUKA\ORUKABundle\Entity\categories $categories
     *
     * @return services
     */
    public function setCategories(\ORUKA\ORUKABundle\Entity\categories $categories = null)
    {
        $this->categories = $categories;

        return $this;
    }

    /**
     * Get categories
     *
     * @return \ORUKA\ORUKABundle\Entity\categories
     */
    public function getCategories()
    {
        return $this->categories;
    }

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

        return $this;
    }

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

    /**
     * Set commune
     *
     * @param string $commune
     *
     * @return services
     */
    public function setCommune($commune)
    {
        $this->commune = $commune;

        return $this;
    }

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions