Symfony 2 - Create Form with Children and Upload File

I am having a problem while trying to create a form that processes an object that is linked to two more objects.

I have a Template object:

class Template
{
  /** 
 * @ORM\Id
 * @ORM\Column(name="template_id", type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
  private $templateId;
  private $name;
}

      

An entity "TemplateMessage" which has a one-way relationship "OneToOne" to "Template", that is, one template can only have one message, but TemplateMessage only knows which template it belongs to:

class TemplateMessage
{
  /** 
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(name="template_message_id", type="integer")
 */
  private $templateMessageId;

  /**
   * @ORM\Column(type="text")
   */
  private $html;

  /**
   * @ORM\Column(type="text")
   */
   private $text;

/**
 * @ORM\OneToOne(targetEntity="CoreBundle\Entity\Template", cascade={"persist"})
 * @ORM\JoinColumn(name="template_id", referencedColumnName="template_id")
 */
private $template;
}

      

And finally, the attachment object for this template:

class TemplateAttachment
{
/** 
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(name="template_attachment_id", type="integer")
 */    
private $templateAttachmentId;

/**
 * @ORM\Column(type="integer")
 */
private $size;

private $file;

/**
 * @ORM\ManyToOne(targetEntity="CoreBundle\Entity\TemplateMessage", cascade={"persist"})
 * @ORM\JoinColumn(name="templateMessageId", referencedColumnName="templateMessageId")
 */
private $templateMessage;

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

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

      

}

After that I have a generated HTML form that handles the creation of the template with its messages and should allow file uploads, all in one form, I need to do it this way (i.e., t break the form)

Well my problem is:

How do I create a form that handles all of this? I read the chapter on Forms in the Symfony book and the form renders OK, but when you try to add a new template, since I need a template ID for the TemplateMessage that is being thrown, the exception is explicitly expressed. My solution was to create a form based NOT on the template, but on the template message, for example:

class TemplateMessageType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('subject', 'text')
            ->add('html', 'textarea')
            ->add('text', 'textarea')
            ->add('template', new TemplateType())
            ->add('file', new TemplateAttachmentType())
       ;
    }
}

      

Being a template Binding type:

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

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'E5P\CoreBundle\Entity\TemplateAttachment'
        ));
    }
}

      

My problem is that the error is being thrown:

Neither the property "file", nor any of the "getFile ()", "file ()", "isFile ()", "hasFile ()", "__get ()" methods exist and are publicly available in the "CoreBundle \ Entity" class \ TemplateMessage "

I don't want to start creating the form with the last child object, but in a "natural" way to create a template and add types for all child objects (those will be TemplateMessage and Template Attachments))

I'm new to Symfony and maybe I'm doing it wrong, any ideas?

Thank you very much in advance!

+3


source to share


1 answer


It looks like the properties of your objects private

, but you don't have getters and setters. I would add getters and setters to the entity classes and go from there.



0


source







All Articles