Symfony 从内部控制器更新表单集合

Symfony Update Form Collection From Inside Controller

我已经找了 4 天了,但在网上找不到任何关于此的信息。我有一个表格收集选票。正上方是它的外观图片。当用户单击“投票”按钮时,我需要更新其正上方的“计数”字段。我如何在 Controller 中执行此操作?当我按下“投票”按钮时,整个表单都会被提交,但我不知道要增加哪个计数 属性。有没有更好的方法来做到这一点?我想将其保留为表单集合,这样我就可以向表单添加一些自定义验证约束。请帮忙!为了澄清起见,我将在下面添加一些我的代码。

ps。如果你们有更好的方法来执行此操作,请告诉我。

投票实体:

<?php
/**
 * Created by PhpStorm.
 * User: joshuacrawmer
 * Date: 11/19/15
 * Time: 11:19 AM
 */

namespace Vote\FoodBundle\Entity;


use Doctrine\Common\Collections\ArrayCollection;

class Votes
{

    protected $votes;

    public function __construct()
    {
        $this->votes = new ArrayCollection();
    }

    public function getVotes()
    {
        return $this->votes;
    }
}

投票实体:

<?php

namespace Vote\FoodBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

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


    /**
     * @var integer
     *
     * @ORM\Column(name="count", type="integer")
     */
    private $count;


    /**
     * @ORM\OneToOne(targetEntity="MonthlySnack", inversedBy="vote")
     */
    private $monthlySnack;


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


    /**
     * Set count
     *
     * @param integer $count
     *
     * @return Vote
     */
    public function setCount($count)
    {
        $this->count = $count;

        return $this;
    }

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

    /**
     * Set monthlySnack
     *
     * @param \Vote\FoodBundle\Entity\MonthlySnack $monthlySnack
     *
     * @return Vote
     */
    public function setMonthlySnack(\Vote\FoodBundle\Entity\MonthlySnack $monthlySnack = null)
    {
        $this->monthlySnack = $monthlySnack;

        return $this;
    }

    /**
     * Get monthlySnack
     *
     * @return \Vote\FoodBundle\Entity\MonthlySnacks
     */
    public function getMonthlySnack()
    {
        return $this->monthlySnack;
    }
}

投票类型:

<?php

namespace Vote\FoodBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class VotesType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('votes', 'collection', array('type' => new VoteType()));

    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'Vote\FoodBundle\Entity\Votes',
            )
        );

    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'vote_foodbundle_votes';
    }
}

投票类型:

<?php

namespace Vote\FoodBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class VoteType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder

            ->add('count', 'text')
            ->add('monthlySnack')
            ->add('vote', 'submit');
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => 'Vote\FoodBundle\Entity\Vote',
            )
        );
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'vote_foodbundle_vote';
    }
}

如果单击任何此按钮,您可以更改 http://symfony.com/doc/current/book/forms.html#book-form-submitting-multiple-buttons

只需制作类似:

<?php
//....
    foreach ($form->get('votes') as $voteForm) {
        if ($voteForm->get('vote')->isClicked()) {
            $vote = $voteForm->getData(); //Here is clicked vote object and u can...
            $vote->increment(); // ... for example increment counter (better than setCount(getCount()+1)
        }
    }