Upload file for each translation

1.1k views Asked by At

I have a form with fields that are translated. One of this fields is a file, witch must to be uploaded (each language have different file).

I am using 'stof/doctrine-extensions-bundle' and 'a2lix/translation-form-bundle' and for uploading files 'vich/uploader-bundle' with symfony2.

found this https://github.com/a2lix/TranslationFormBundle/issues/83, but it's not working

Maximum function nesting level of '100' reached, aborting!

My Catalog entity:

/**
 * Catalog.
 *
 * @ORM\Entity()
 * @ORM\Table("catalog")
 *
 * @Vich\Uploadable
 *
 * @Gedmo\TranslationEntity(class="Entity\Catalog\CatalogTranslation")
 */
class Catalog
{

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

/**
 * @var string
 *
 * @ORM\Column()
 *
 * @Gedmo\Translatable()
 *
 * @Assert\NotBlank()
 */
private $name;
/**
 * @var File
 *
 * @Vich\UploadableField(mapping="product_image", fileNameProperty="imageName")
 *
 * @Assert\Image(maxSize = "4M")
 */
protected $imageFile;

/**
 * @var string
 *
 * @ORM\Column(nullable=true)
 * @Gedmo\Translatable()
 */
protected $imageName;
/**
 * @ORM\OneToMany(
 *   targetEntity="CatalogTranslation",
 *   mappedBy="catalog",
 *   cascade={"persist", "remove"}
 * )
 */
private $translations;
}

Also tried this when building form:

->add('imageName', 'a2lix_translationsForms', [
            'form_type' => 'vich_file',
            'form_options' => array(
                'required' => true,
                'mapping' => 'product_image',
                'allow_delete' => true,
                'download_link' => true,
            )
        ])
1

There are 1 answers

0
K-Phoen On

My guess is that CatalogTranslation should be the class defined as Uploadable as otherwise it will probably confuse VichUploaderBundle.

I'd try something like this:

<?php

/**
 * Catalog.
 *
 * @ORM\Entity()
 * @ORM\Table("catalog")
 *
 * NOT uploadable
 *
 * @Gedmo\TranslationEntity(class="Entity\Catalog\CatalogTranslation")
 */
class Catalog
{
    /**
     * @var int
     *
     * @ORM\Column(type="integer")
     * @ORM\Id()
     * @ORM\GeneratedValue()
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column()
     *
     * @Gedmo\Translatable()
     *
     * @Assert\NotBlank()
     */
    private $name;

    /**
     * @var string
     *
     * @ORM\Column(nullable=true)
     * @Gedmo\Translatable()
     */
    protected $imageName;
    /**
     * @ORM\OneToMany(
     *   targetEntity="CatalogTranslation",
     *   mappedBy="catalog",
     *   cascade={"persist", "remove"}
     * )
     */
    private $translations;
}

class CatalogTranslation
{
    // ... as usual

    /**
     * @var File
     *
     * @Vich\UploadableField(mapping="product_image", fileNameProperty="imageName")
     *
     * @Assert\Image(maxSize = "4M")
     */
    protected $imageFile;


    protected $imageName;
}