简体   繁体   English

Symfony2-创建表单需要传递登录用户

[英]Symfony2 - Creating a form need to pass in logged in user

I have a reply form setup for replies to posts. 我有用于回复帖子的回复表单设置。

I want to setup the author to be the user currently logged in. Right now it's just a drop down menu of previous authors that's already posted, which isn't what I want. 我想将作者设置为当前登录的用户。现在,这只是已发布的先前作者的下拉菜单,这不是我想要的。

How do I set it up to use the current logged in username as an author instead having a list of authors already saved to the database? 如何设置它以使用当前登录的用户名作为作者,而不是已经将一个作者列表保存到数据库中?

Is there a global command to access the currently logged in user to pass in to the ->add('author) line in the form? 是否存在全局命令来访问当前登录的用户以传递至表单中的->add('author)行?

ReplyType form ReplyType表格

class ReplyType extends AbstractType
{
/**
 * {@inheritDoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('author')
        ->add('body')
        ->add('post', 'submit');
}

/**
 * {@inheritDoc}
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Acme\DemoBundle\Entity\Reply'
    ));
}

/**
 * {@inheritDoc}
 */
public function getName()
{
    return 'acme_demobundle_reply';
}
}

Reply entity 回复实体

/**
 * @var Author
 *
 * @ORM\ManyToOne(targetEntity="Author", inversedBy="replies")
 * @ORM\JoinColumn(name="author_id", referencedColumnName="id", nullable=false)
 * @Assert\NotBlank
 */
private $author;

Since the logged in user is the author, how about removing the field and setting the author to the currently logged in user inside the controller? 由于登录用户是作者,因此如何删除字段并将作者设置为控制器内当前登录的用户呢? The user should not actually select the author (ie: see Stackoverflow.com reply or comment forms). 用户实际上不应选择作者(即:请参阅Stackoverflow.com的答复或评论表格)。

// ...
use Symfony\Component\HttpFoundation\Request;

public function postReplyAction(Request $request)
{
    $reply = new Reply();

    $form = $this->createForm(new ReplyType(), $reply);

    $form->handleRequest($request);

    if ($form->isValid()) {
        // perform your logic and before savig the reply to the database
        $reply->setAuthor($this->getUser());
        // save your reply

        return $this->redirect($this->generateUrl('reply_success'));
    }

    // ...
}

And your form type becomes: 您的表单类型变为:

class ReplyType extends AbstractType
{

    /**
     * {@inheritDoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('body')
            ->add('post', 'submit');
    }

    /**
     * {@inheritDoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\DemoBundle\Entity\Reply'
        ));
    }

    /**
     * {@inheritDoc}
     */
    public function getName()
    {
        return 'acme_demobundle_reply';
    }

}

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

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