简体   繁体   English

以Symfony形式为实体设置字段

[英]Setting A Field to Entity in Symfony Form

In my project, I have two entities. 在我的项目中,我有两个实体。 Post and Comment. 发表和评论。

Comment entity has following fields Comment实体具有以下字段

  • name 名称
  • message 信息
  • post 发布

I'm rendering comment form just after post. 我在发布后立即呈现评论表单。 Presently in comment form I'm getting a drop down menu from which I can select a post. 目前,我以评论形式获得一个下拉菜单,从中可以选择一个帖子。 I don't that. 我不是那样 I want to set post field to a default value which is same as the post rendered above that. 我想将发布字段设置为默认值,该默认值与在其上方呈现的发布相同。 And it should not be changeable. 而且它不应更改。
How can I achieve this behavior? 我该如何实现这种行为?

UPDATE 更新

I have following code in my controller $comment = new Comment(); 我的控制器中有以下代码$ comment = new Comment();

$comment -> setPost($entity);
$commentForm = $this -> createCommentForm($comment);
return array(
  'entity'      => $entity,
  'delete_form' => $deleteForm->createView(),
  'comment_form' => $commentForm->createView()
);

In this code, I'm setting post value, which is coming as default value in form, but it is changeable. 在这段代码中,我设置了post值,它是表单中的默认值,但是它是可变的。

I'm creating form using following code: 我正在使用以下代码创建表单:

public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder
    ->add('name')
    ->add('reference')
    ->add('post','entity', array(
       'class' => 'OpenSourceFeedBundle:Post',
       'required' => TRUE
     ))
    ->add('date', 'date', array(
      'html5' => TRUE,
      'widget' => 'single_text',
      'read_only' => TRUE
    ))
    ->add('message')
    ;
  }

You should set up comment $post value in controller. 您应该在控制器中设置comment $ post值。 Can you show me your post/show action of your Post controller? 您能告诉我您的Post控制器的发布/显示操作吗?

Basically you have show action of the post, in your controller you have post object available, so when post trigger you should do something like this 基本上,您可以显示帖子的动作,在控制器中,您可以使用帖子对象,因此在发布触发器时,您应该执行以下操作

$comment = new Comment();
$comment->setPost($post);

and then do what ever you need to do, like flush comment to the database. 然后执行所需的操作,例如对数据库进行刷新注释。

In twig you should not display that part with dropdown. 在树枝中,您不应使用下拉菜单显示该部分。

If you will be able to paste twig and controller I will show you solution. 如果您能够粘贴树枝和控制器,我将向您显示解决方案。

Try this 试试这个

disabled 残障人士

type: boolean default: false 类型:布尔默认值:false

If you don't want a user to modify the value of a field, you can set the disabled option to true. 如果您不希望用户修改字段的值,则可以将Disabled选项设置为true。 Any submitted value will be ignored. 任何提交的值将被忽略。

public function buildForm(FormBuilderInterface $builder, array $options)
  {
    $builder
    ->add('name')
    ->add('reference')
    ->add('post','entity', array(
       'class' => 'OpenSourceFeedBundle:Post',
       'required' => TRUE,
       'disabled' => true,
     ))
    ->add('date', 'date', array(
      'html5' => TRUE,
      'widget' => 'single_text',
      'read_only' => TRUE
    ))
    ->add('message')
    ;
  }

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

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