symfony2admingenerator / FormExtensionsBundle

Symfony2 form extensions for Admingenerator project (also working standalone!)
Other
13 stars 13 forks source link

Collection upload help - sf3 #64

Closed pjam closed 8 years ago

pjam commented 8 years ago

Hi all

I'm trying to use the collection upload extension, but I can't get it to work following the current documentation. My files:

Projecto.php

<?php

namespace AdminBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * Projecto
 *
 * @ORM\Table(name="projecto")
 * @ORM\Entity(repositoryClass="AdminBundle\Repository\ProjectoRepository")
 */
class Projecto
{
    /**
     * @ORM\OneToMany(targetEntity="ImagemProjecto", mappedBy="projecto", cascade={"all"}, orphanRemoval=true)
     * @ORM\OrderBy({"position" = "ASC"})
     */
    protected $imagensProjecto;

    public function __construct()
    {
      $this->imagensProjecto = new ArrayCollection();
    }

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

    /**
     * @var string
     *
     * @ORM\Column(name="designacao", type="string", length=255, unique=true)
     */
    private $designacao;

    /**
     * @var string
     *
     * @ORM\Column(name="descricao", type="text")
     */
    private $descricao;

    /**
     * @var string
     *
     * @ORM\Column(name="titulo", type="string", length=255)
     */
    private $titulo;

    /**
     * @var string
     *
     * @Gedmo\Slug(fields={"designacao"})
     * @ORM\Column(name="slug", type="string", length=255)
     */
    private $slug;

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

    /**
     * Set designacao
     *
     * @param string $designacao
     *
     * @return Projecto
     */
    public function setDesignacao($designacao)
    {
        $this->designacao = $designacao;

        return $this;
    }

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

    /**
     * Set descricao
     *
     * @param string $descricao
     *
     * @return Projecto
     */
    public function setDescricao($descricao)
    {
        $this->descricao = $descricao;

        return $this;
    }

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

    /**
     * Set titulo
     *
     * @param string $titulo
     *
     * @return Projecto
     */
    public function setTitulo($titulo)
    {
        $this->titulo = $titulo;

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Add imagensProjecto
     *
     * @param \AdminBundle\Entity\ImagemProjecto $imagensProjecto
     *
     * @return Projecto
     */
    public function addImagensProjecto(\AdminBundle\Entity\ImagemProjecto $imagensProjecto)
    {
        $this->imagensProjecto[] = $imagensProjecto;

        return $this;
    }

    /**
     * Remove imagensProjecto
     *
     * @param \AdminBundle\Entity\ImagemProjecto $imagensProjecto
     */
    public function removeImagensProjecto(\AdminBundle\Entity\ImagemProjecto $imagensProjecto)
    {
        $this->imagensProjecto->removeElement($imagensProjecto);
    }

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

ImagemProjecto.php

<?php

namespace AdminBundle\Entity;

use Admingenerator\FormExtensionsBundle\Form\Model\UploadCollectionFileInterface;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
 * @ORM\Entity(repositoryClass="Gedmo\Sortable\Entity\Repository\SortableRepository")
 * @Vich\Uploadable
 */
class ImagemProjecto implements UploadCollectionFileInterface
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

    /**
     * (Optional) nameable field
     *
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $name;

    /**
     * (Optional) additional editable field
     *
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $description;

    /**
     * (Optional) sortable group
     *
     * @Gedmo\SortableGroup
     * @ORM\ManyToOne(targetEntity="Projecto", inversedBy="imagensProjecto")
     * @ORM\JoinColumn(name="projecto_id", referencedColumnName="id")
     */
    protected $projecto;

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

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

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

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

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

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

    /**
     * Set path
     *
     * @param string $path
     *
     * @return ImagemProjecto
     */
    public function setPath($path)
    {
        $this->path = $path;

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

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

        return $this;
    }

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

    /**
     * Set projecto
     *
     * @param \AdminBundle\Entity\Projecto $projecto
     *
     * @return ImagemProjecto
     */
    public function setProjecto(\AdminBundle\Entity\Projecto $projecto = null)
    {
        $this->projecto = $projecto;

        return $this;
    }

    /**
     * Get projecto
     *
     * @return \AdminBundle\Entity\Projecto
     */
    public function getProjecto()
    {
        return $this->projecto;
    }

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

Projecto-generator.yml

generator: admingenerator.generator.doctrine
params:
    model: AdminBundle\Entity\Projecto
    namespace_prefix: 
    concurrency_lock: ~
    bundle_name: AdminBundle
    pk_requirement: ~
    fields:
        imagensProjecto:
            label:            Images
            dbType:           collection
            # changed here for the new sf notation
            formType:         Admingenerator\FormExtensionsBundle\Form\Type\UploadCollectionType
            addFormOptions:
                primary_key:          id
                nameable:             true
                nameable_field:       name
                sortable:             true
                sortable_field:       position
                editable:             [ description ]
                #type:                 \AdminBundle\Form\Type\ImagemProjecto\EditType
                maxNumberOfFiles:     5
                maxFileSize:          500000
                minFileSize:          1000
                acceptFileTypes:      /(\.|\/)(gif|jpe?g|png)$/i
                loadImageFileTypes:   /^image\/(gif|jpe?g|png)$/i
                loadImageMaxFileSize: 250000
                previewMaxWidth:      100
                previewMaxHeight:     100
                previewAsCanvas:      true
                prependFiles:         false
                allow_add:            true
                allow_delete:         true
                error_bubbling:       false
                options:
                  data_class:       AdminBundle\Entity\ImagemProjecto
    object_actions:
        delete: ~
    batch_actions:
        delete: ~
builders:
    list:
        params:
            title: List for AdminBundle
            display: ~
            actions:
                new: ~
            object_actions:
                edit: ~
                delete: ~
    excel:
        params: ~
        filename: ~
        filetype: ~
    new:
        params:
            title: New object for AdminBundle
            display: ~
            actions:
                save: ~
                list: ~
    edit:
        params:
            title: "You're editing the object \"%object%\"|{ %object%: Projecto.title }|"
            display: ~
            actions:
                save: ~
                list: ~
    show:
        params:
            title: "You're viewing the object \"%object%\"|{ %object%: Projecto.title }|"
            display: ~
            actions:
                list: ~
                new: ~
    actions:
        params:
            object_actions:
                delete: ~
            batch_actions:
                delete: ~

When I open the new form, I get The option "options" does not exist. Defined options are: ... (the same if I introduce the type option). After this I tried adding the option "options" to the UploadCollectionType, and I got this: Key "description" in object with ArrayAccess of class "Symfony\Component\Form\FormView" does not exist in AdmingeneratorFormExtensionsBundle:Form/UploadCollection:template_download.html.twig at line 8

If I remove description from the editable option, I get: Key "id" in object with ArrayAccess of class "Symfony\Component\Form\FormView" does not exist in AdmingeneratorFormExtensionsBundle:Form/UploadCollection:template_download.html.twig at line 70

Should I just use the bootstrap collection extension instead? I'm stuck at this, so I would like some help, if possible.

pjam commented 8 years ago

I finally figured it out.

For future reference and until I have time to make a PR (or someone else), I'll put here what needs to be done. First, the generator options "options" and "type" are now "entry_options" and "entry_type". Then, there are some corrections to be made in the bundle.

This is what I've found and it seems to be working. I think at least some of these issues are common to the Bootstrap Collection extension.

I hope it helps.

bobvandevijver commented 8 years ago

@pjam Great you've found the problem, we'll be waiting for the PR :smiley: