简体   繁体   English

Symfony 2 - 使用子实体和上传文件创建表单

[英]Symfony 2 - Create Form with child entities & upload file

I'm having a problem when trying to create a Form that handles an Entity that is related to 2 more Entities. 我在尝试创建一个处理与另外两个实体相关的实体的表单时遇到问题。

I have a "Template" entity: 我有一个“模板”实体:

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 "OneToOne" uni directional relationship with "Template", this is, one Template can have only one Message, but the TemplateMessage only know to which Template it belongs: 一个实体“TemplateMessage”,它与“模板”具有“OneToOne”单向关系,这是一个模板只能有一个消息,但是TemplateMessage只知道它属于哪个模板:

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, an entity for attachments for that 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 this, I have an HTML form created, that handles the creation of the Template, with its messages and should allow to upload files, all in a single form, I need to do it that way (That is, can't break the form) 在此之后,我创建了一个HTML表单,它处理模板的创建及其消息,并且应该允许上传文件,所有这些都在一个表单中,我需要这样做(也就是说,不能打破形成)

Well, my problem is: 好吧,我的问题是:

How do I create a Form that handles all this? 如何创建处理所有这些的表单? I read the chapter about Forms in the Symfony Book, and the form render OK, but when try to add a new Template, as I need a Template ID for the TemplateMessage to be created, an Exception obviously is raised. 我阅读了有关Symfony Book中的表单的章节,并且表单呈现正常,但是当尝试添加新模板时,因为我需要为要创建的TemplateMessage创建模板ID,所以显然会引发异常。 My solution was to create a Form based NOT in the Template but in the Template Message like this: 我的解决方案是在模板中创建一个基于表单的NOT,但在模板消息中创建如下:

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 the TemplateAttachmentType: 作为TemplateAttachmentType:

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 an error is thrown: 我的问题是抛出了一个错误:

Neither the property "file" nor one of the methods "getFile()", "file()", "isFile()", "hasFile()", "__get()" exist and have public access in class "CoreBundle\\Entity\\TemplateMessage" 属性“文件”和方法之一“getFile()”,“file()”,“isFile()”,“hasFile()”,“__ get()”都不存在,并且在类“CoreBundle”中具有公共访问权限实体\\ TemplateMessage”

I don't want to start the creation of the Form from the last child entity, but in a "natural" way of creating a Template and add the Types for all the child entities (Those would be TemplateMessage & Template Attachments") 我不想从最后一个子实体开始创建Form,而是以“自然”方式创建模板并为所有子实体添加类型(那些将是TemplateMessage和Template Attachments“)

I'm new to Symfony, and maybe I'm doing the things in a wrong way, any ideas? 我是Symfony的新手,也许我正在以错误的方式做事,有什么想法吗?

Thanks very much in advance! 首先十分感谢!

It appears that you entities' properties are private , but you don't have getters and setters. 您的实体属性似乎是private ,但您没有getter和setter。 I'd add getters and setters to the entity classes and go from there. 我将getter和setter添加到实体类中并从那里开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM