symfony2admingenerator / AdmingeneratorGeneratorBundle

(old-legacy) Admingenerator for Symfony2, parse generator.yml files to build classes
http://symfony2admingenerator.org/
MIT License
360 stars 125 forks source link

Problem Multi upload with single upload #815

Open rpostolov opened 9 years ago

rpostolov commented 9 years ago

Hi,

I have a form in which i can upload one picture as a pricnipal picture and just below i added a multi upload which allow to upload more pictures but the form can't be saved when i'm using both in the same time. When i try with only one of them it works perfectly.

So if you can help me because i'm on it for two days.

Here is my code :

Edito entity :

<?php

namespace Tranoi\EditoBundle\Entity;

use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Translatable;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Edito
 *
 * @ORM\Table(name="edito")
 * @ORM\Entity(repositoryClass="Tranoi\EditoBundle\Entity\EditoRepository")
 * @Vich\Uploadable 
 * @ORM\HasLifecycleCallbacks() 
 */
class Edito implements Translatable
{

    /**
     * @Gedmo\Locale
     * Used locale to override Translation listener`s locale
     * this is not a mapped field of entity metadata, just a simple property
     */
    private $locale;

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

    /**
     * @var integer
     * @ORM\Column(name="statut", type="boolean", nullable=false)
     */
    private $statut;

    /**
     * @var string
     * @Gedmo\Translatable
     * @ORM\Column(name="auteur", type="string", length=255, nullable=false)
     */
    private $auteur;

    /**
     * @var string
     * @Gedmo\Translatable
     * @ORM\Column(name="titre", type="string", length=255, nullable=false)
     */
    private $titre;

    /**
     * @var string
     * @Gedmo\Translatable
     * @ORM\Column(name="resume", type="text", nullable=true)
     */
    private $resume;

    /**
     * @var string
     * @Gedmo\Translatable
     * @ORM\Column(name="texte", type="text", nullable=false)
     */
    private $texte;

    /**
     * @Vich\UploadableField(mapping="edito_image", fileNameProperty="image")
     * @var File $imageFile
     */
    protected $imageFile;

    /**
     * @var string
     * @ORM\Column(name="image", type="string", length=100, nullable=true)
     */
    private $image;

    /**
     * Affiche l'icone
     */
    public $ImageShow; 

    /**
     * @var ArrayCollection Gallery $gallery
     * Owning Side
     * @Assert\Count(
     *      min = "1",
     *      max = "50",
     *      minMessage = "You must add at least 1 image",
     *      maxMessage = "You should not add more than 50 pictures"
     * )
     * @ORM\OneToMany(targetEntity="Tranoi\EditoBundle\Entity\Gallery", mappedBy="edito", orphanRemoval=true, cascade={"persist", "remove"})
     * @ORM\OrderBy({"position" = "ASC"})
     */
    private $gallery;

    /**
     * @var \DateTime
     * @ORM\Column(name="date_creation", type="date", nullable=false)
     */
    private $dateCreation;

    /**
     * @var \DateTime
     * @ORM\Column(name="date_modification", type="date", nullable=false)
     */
    private $dateModification;

    /** 
     * @var \DateTime 
     * @ORM\Column(name="created_at", type="datetime", nullable=false) 
     */  
    protected $createdAt;  

    /** 
     * @var \DateTime 
     * @ORM\Column(name="updated_at", type="datetime", nullable=false) 
     */  
    protected $updatedAt;

    /**
     * @Gedmo\Slug(fields={"titre"})
     * @ORM\Column(name="slug", length=128, unique=true, nullable=false)
     */
    private $slug; 

    /**
     * Constructor
     */
    public function __construct()
    {        
        $this->createdAt = new \DateTime('now');         
        $this->updatedAt = new \DateTime('now');

        $this->gallery = new \Doctrine\Common\Collections\ArrayCollection();

        $this->dateCreation = new \DateTime('now');         
        $this->dateModification = new \DateTime('now');
    }

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

    /**
     * Set titre
     *
     * @param string $titre
     * @return Edito
     */
    public function setTitre($titre)
    {
        $this->titre = $titre;

        return $this;
    }

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

    /**
     * Set auteur
     *
     * @param string $auteur
     * @return Edito
     */
    public function setAuteur($auteur)
    {
        $this->auteur = $auteur;

        return $this;
    }

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

    /**
     * Set statut
     *
     * @param integer $statut
     * @return Edito
     */
    public function setStatut($statut)
    {
        $this->statut = $statut;

        return $this;
    }

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

    /**
     * Set resume
     *
     * @param string $resume
     * @return Edito
     */
    public function setResume($resume)
    {
        $this->resume = $resume;

        return $this;
    }

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

    /**
     * Set texte
     *
     * @param string $texte
     * @return Edito
     */
    public function setTexte($texte)
    {
        $this->texte = $texte;

        return $this;
    }

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

    /**
     * Set image
     *
     * @param string $image
     * @return Edito
     */
    public function setImage($image)
    {
        $this->image = $image;

        return $this;
    }

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

    /**
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
     * must be able to accept an instance of 'File' as the bundle will inject one here
     * during Doctrine hydration.
     *
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
     */
    public function setImageFile(/*File*/ $image )
    {
        $this->imageFile = $image;

        if ($image) {
            $this->updatedAt = new \DateTime('now');
        }
    }

    /**
     * @return File
     */
    public function getImageFile()
    {
        return $this->imageFile;
    }

    public function getImageShow(){ 
        if($this->image){        
            echo "<img src='/media/cache/resolve/thumb_admin_list/images/edito/".$this->image."' />";
        }        
    }  

    /**
     * Add gallery
     *
     * @param \Tranoi\EditoBundle\Entity\Gallery $gallery
     * @return Edito
     */
    public function addGallery(\Tranoi\EditoBundle\Entity\Gallery $gallery)
    {
        $this->gallery[] = $gallery;

        return $this;
    }

    /**
     * Remove gallery
     *
     * @param \Tranoi\EditoBundle\Entity\gallery $gallery
     */
    public function removeGallery(\Tranoi\EditoBundle\Entity\Gallery $gallery)
    {
        $this->gallery->removeElement($gallery);
    }

    /**
     * Get gallery
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getGallery()
    {
        return $this->gallery;
    } 

    /**
     * Set dateCreation
     *
     * @param \DateTime $dateCreation
     * @return Edito
     */
    public function setDateCreation($dateCreation)
    {
        $this->dateCreation = $dateCreation;

        return $this;
    }

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

    /** 
     * Set createdAt 
     * 
     * @ORM\PrePersist 
     */  
    public function setCreatedAt()  
    {  
        $this->createdAt = new \DateTime();  
        $this->updatedAt = new \DateTime();  
    }  

    /** 
     * Get CreatedAt 
     * 
     * @return \DateTime 
     */  
    public function getCreatedAt()  
    {  
        return $this->createdAt;  
    }      

    /** 
     * Set updatedAt 
     * 

     */  
    public function setUpdatedAt()  
    {  
        $this->updatedAt = new \DateTime();  
    } 

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

    /**
     * Set dateModification
     *
     * @param \DateTime $dateModification
     * @return Edito
     */
    public function setDateModification($dateModification)
    {
        $this->dateModification = $dateModification;

        return $this;
    }

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

    /**
     * Set slug
     *
     * @param string $slug
     * @return Edito
     */
    public function setSlug($slug)
    {
        $this->slug = $slug;

        return $this;
    }

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

}

Gallery entity

<?php

namespace Tranoi\EditoBundle\Entity;

use Avocode\FormExtensionsBundle\Form\Model\UploadCollectionFileInterface;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Translatable\Translatable;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;

/**
 * Gallery
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
 * @ORM\Entity(repositoryClass="Tranoi\EditoBundle\Entity\GalleryRepository")
 * @Vich\Uploadable 
 * @ORM\HasLifecycleCallbacks() 
 */
class Gallery implements Translatable, UploadCollectionFileInterface
{

    /**
     * @var Edito $edito
     * @Gedmo\SortableGroup
     * @ORM\ManyToOne(targetEntity="Tranoi\EditoBundle\Entity\Edito", inversedBy="gallery")
     * @ORM\JoinColumn(name="edito_id", referencedColumnName="id")
     */
    protected $edito;

    /**
     * @Gedmo\Locale
     * Used locale to override Translation listener`s locale
     * this is not a mapped field of entity metadata, just a simple property
     */
    private $locale;

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @Vich\UploadableField(mapping="gallery_images_edito", fileNameProperty="path")
     */
    protected $file;

    /**
     * @var string
     * @ORM\Column(name="path", type="string", length=100, nullable=true)
     */
    protected $path;

    /**
     * @var string
     * @Gedmo\Translatable
     * @ORM\Column(name="legende", type="text", nullable=true)
     */
    protected $legende;

    /**
     * @var integer
     * @Gedmo\SortablePosition
     * @ORM\Column(name="position", type="integer", nullable=true)
     */
    protected $position;

    /** 
     * @var \DateTime 
     * @ORM\Column(name="created_at", type="datetime", nullable=false) 
     */  
    protected $createdAt;  

    /** 
     * @var \DateTime 
     * @ORM\Column(name="updated_at", type="datetime", nullable=false) 
     */  
    protected $updatedAt; 

    public function __construct()
    {
    }

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

    /**
     * Set legende
     *
     * @param string $legende
     * @return Gallery
     */
    public function setLegende($legende)
    {
        $this->legende = $legende;

        return $this;
    }

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

    /**
     * Set position
     *
     * @param integer $position
     * @return Gallery
     */
    public function setPosition($position)
    {
        $this->position = $position;

    }

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

    /** 
     * Set createdAt 
     * 
     * @ORM\PrePersist 
     */  
    public function setCreatedAt()  
    {  
        $this->createdAt = new \DateTime();  
        $this->updatedAt = new \DateTime();  
    }  

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

    /** 
     * Set updatedAt 
     * 
     * @ORM\PreUpdate 
     */  
    public function setUpdatedAt()  
    {  
        $this->updatedAt = new \DateTime();  
    }  

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

    /**
     * Set edito
     *
     * @param \Tranoi\EditoBundle\Entity\Edito $edito
     * @return Gallery
     */
    public function setEdito(\Tranoi\EditoBundle\Entity\Edito $edito)
    {
        $this->edito = $edito;

        return $this;
    }

    /**
     * Get edito
     *
     * @return \Tranoi\EditoBundle\Entity\Edito
     */
    public function getEdito()
    {
        return $this->edito;
    }

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

    public function getPath() {
        return $this->path;
    }

    public function setFile(\Symfony\Component\HttpFoundation\File\File $file) {
        $this->file = $file;
    }

    public function getFile() {
        return $this->file;
    }

    public function getSize() {
        return $this->file->getFileInfo()->getSize();
    }

    public function setParent($parent) {
        $this->setEdito($parent);
    }

    public function getPreview() {
        return (preg_match('/image\/.*/i', $this->file->getMimeType()));
    }

    public function __toString(){
        return $this->path;
    }   

}

EditoAmin-genrator.yml

generator: admingenerator.generator.doctrine
params:
    model: Tranoi\EditoBundle\Entity\Edito
    namespace_prefix: Tranoi
    concurrency_lock: ~
    bundle_name: EditoBundle
    embed_types:
       # short syntax
       - "GalleryAdmin-generator.yml" 
    pk_requirement: ~
    fields:
      id:
        credentials: 'hasRole("ROLE_COMMERCIAL")' 
      statut: 
        credentials: 'hasRole("ROLE_COMMERCIAL")' 
        label: status 
        formType: choice
        addFormOptions:
          choices:
            0: Draft
            1: in line  
      auteur:
        label:  Author
      titre:
        label:  Title
      resume:
        label:  Resume
      texte:
        label:  Text
        formtype: textarea
      imageFile:
        label:            Image
        formType:         afe_single_upload
        dbType:           string
        addFormOptions:
           deleteable:     image
           data_class:     Symfony\Component\HttpFoundation\File\File 
           previewImages: true
           previewAsCanvas: true
           previewFilter: thumb_admin_list
      ImageShow:
        label:            Image
      path:
        formType:  hidden 
      position:
        formType:  hidden         
      gallery:
        label:            '1 Picture Minimum and 50 Pictures Maximum - Maximum 3 Mo / picture - PNG or JPG'
        dbType:           collection
        formType:         afe_collection_upload
        addFormOptions:
            attr:
              accept: "image/jpeg, image/png"  # http://stackoverflow.com/questions/11832930/html-input-file-accept-attribute-file-type-csv/11834872#11834872
            primary_key:          id
            nameable:             false
            sortable:             true
            sortable_field:       position
            editable:             [ path, position ]
            type:                 \Tranoi\EditoBundle\Form\Type\GalleryAdmin\EditType
            maxNumberOfFiles:     50
            maxFileSize:          5000000
            minFileSize:          1000
            acceptFileTypes:      /(\.|\/)(gif|jpe?g|png)$/i
            loadImageFileTypes:   /^image\/(gif|jpe?g|png)$/i
            loadImageMaxFileSize: 2500000
            previewMaxWidth:      100
            previewMaxHeight:     100
            previewAsCanvas:      true
            previewFilter:        thumb_gallery_images_admin_list
            prependFiles:         true
            allow_add:            true
            allow_delete:         true
            error_bubbling:       false
            options:
                data_class:       Tranoi\EditoBundle\Entity\Gallery
    object_actions:
        delete: ~
    batch_actions:
        delete: ~
builders:
    list:
        params:
            title: List for Edito
            display: [statut,ImageShow,id,auteur,titre]
            actions:
                new: ~
            object_actions:
                edit: ~
                delete: ~
    excel:
        params: ~
        filename: ~
        filetype: ~
    filters:
        params:
            display: [statut,id,auteur,titre]
    new:
        params:
            title: New Edito
            display: [statut,auteur,titre,resume,texte,imageFile,gallery]
            actions:
                save: ~
                list: ~
    edit:
        params:
            title: "You're editing the edito \"%object%\"|{ %object%: Edito.titre }|"
            display: [statut,auteur,titre,resume,texte,imageFile,gallery]
            actions:
                save: ~
                list: ~
    show:
        params:
            title: "You're viewing the edito \"%object%\"|{ %object%: Edito.titre }|"
            display: ~
            actions:
                list: ~
                new: ~
    actions:
        params:
            object_actions:
                delete: ~
            batch_actions:
                delete: ~

GalleryAdmin-generator.yml

generator: admingenerator.generator.doctrine
params:
    model: Tranoi\EditoBundle\Entity\Gallery
    namespace_prefix: Tranoi
    concurrency_lock: ~
    bundle_name: EditoBundle
    pk_requirement: ~
    fields:
      createdAt:
        label: Date de création    
        formType:  afe_date_picker     
      updatedAt:
        label: Date mise à jour    
        formType:  afe_date_picker  
      position:
        formType:  hidden   
      path:
        formType:  hidden     

    object_actions:
        delete: ~
    batch_actions:
        delete: ~
builders:
    list:
        params:
            title: List for EditoBundle
            display: ~
            actions:
                new: ~
            object_actions:
                edit: ~
                delete: ~
    excel:
        params: ~
        filename: ~
        filetype: ~
    filters:
        params:
            display: ~
    new:
        params:
            title: New object for EditoBundle
            display: ~
            actions:
                save: ~
                list: ~
    edit:
        params:
            title: "You're editing the object \"%object%\"|{ %object%: Gallery.id }|"
            display: ~
            actions:
                save: ~
                list: ~
    show:
        params:
            title: "You're viewing the object \"%object%\"|{ %object%: Gallery.id }|"
            display: ~
            actions:
                list: ~
                new: ~
    actions:
        params:
            object_actions:
                delete: ~
            batch_actions:
                delete: ~

Thanks :)

sescandell commented 9 years ago

Hi @rpostolov

Do you have any error message or anything else?

I have a sample using single upload and collecetion upload working well and available in this repository sample for example: https://github.com/sescandell/CollectionUploadSample

The Product generator: https://github.com/sescandell/CollectionUploadSample/blob/master/src/Acme/DemoBundle/Resources/config/Product-generator.yml use both single_upload and collection_upload (through the library form)

Otherwise, I should be able to dive into you codes next week (I'm not available before that)

Thanks,

rpostolov commented 9 years ago

Hi,

Thanks for your answer and don't worry look at this when you have time :)

Unfortunately i don't have any other errors message.

I'm looking to your file.

Thanks

ioleo commented 9 years ago

@rpostolov I'm useing single upload and collection upload in the same form, and it works :ok:

However I'm useing the new-unstable repo, and there have been some minor fixes to both form types recently.

By the afe_ prefix I can tell you're useing the old-legacy repo. Can you tell me which version are you iseing? latest tag 0.1.4 ? or @dev-master or maybe some other?

rpostolov commented 9 years ago

Hi loostro,

I am using @dev-master, but now it's weird because few days ago it didn't work i cleared the cache try again and nothig. Now it work, i have only on error about array key not exist in SingleUploadSubscriber.php from avocode but it's not blocking the action. I don't undurstand why it's working now and why not before...