1

I'm using A2lix Translation Form Bundle and Doctrine Behaviors Translatable in a project where I have two entities: company and files. Company has some translatable fields so I have a CompanyTranslations Entity for that. One company can have one file so Company and file are mapped with an OneToOne unidirectional reference. The company file is translatable so the property is in the CompanyTranslation file.

CompanyTranslation:

class CompanyTranslation
{
    use ORMBehaviors\Translatable\Translation;

    /**
     * @ORM\OneToOne(targetEntity="File", cascade={"persist"})
     * @ORM\JoinColumn(name="translatable_file_id", referencedColumnName="id")
     * @Assert\Valid()
     * @Assert\Type(type="MyApp\CoreBundle\Entity\File")
     **/
    private $translatableFile;

    /**
     * Set translatableFile
     *
     * @param $translatableFile
     */
    public function setTranslatableFile(File $translatableFile = null)
    {
        $this->translatableFile = $translatableFile;
    }

    /**
     * Get translatableFile
     *
     * @return $translatableFile
     */
    public function getTranslatableFile()
    {
        return $this->translatableFile;
    }
}

File:

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

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

    /**
     * @Assert\File()
     */
    private $file;

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

    /**
     * Set filePath
     *
     * @param string $filePath
     */
    public function setFilePath($filePath)
    {
        $this->filePath = $filePath;
    }

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

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

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

File Form Type:

class FileType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('file', 'file', array(
            'label' => false
        ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MyApp\CoreBundle\Entity\File'
        ));
    }

    public function getName()
    {
        return 'file_form';
    }
}

Company Form Type:

class CompanyType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('translations', 'a2lix_translationsForms', array(
                'locales' => $this->languages,
                'form_type' => new FileType(),
                'form_options' => array(
                    'data_class' => 'MyApp\CoreBundle\Entity\File',
                    'required'      => false,
                    'validation_groups' => array('file_upload')
                )
            ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        parent::setDefaultOptions($resolver);

        $resolver->setDefaults(array(
            'data_class' => 'MyApp\CoreBundle\Entity\Company'
        ));
    }
}

The error is this one:

The form's view data is expected to be an instance of class MyApp\CoreBundle\Entity\File, but is an instance of class MyApp\CoreBundle\Entity\CompanyTranslation. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class MyApp\CoreBundle\Entity\CompanyTranslation to an instance of MyApp\CoreBundle\Entity\File.

I already set the data_class of the File Type Form and the data_class of the field to null but also to MyApp\CoreBundle\Entity\File. Both send me errors. I don't know what's happening.

Could anyone help?

Thanks!

ana-lu
  • 269
  • 1
  • 3
  • 12

0 Answers0