来自内部控制器

时间:2015-11-21 05:42:36

标签: symfony symfony-forms

Image of the form collection

我一直在寻找4天,但在网上找不到任何关于此的内容。我有一张表格集合。正上方是一幅它的样子。当用户单击“投票”按钮时,我需要更新其正上方的“计数”字段。我如何在Controller内执行此操作?当我按下投票按钮时,整个表单都会被提交,我不知道要递增哪个Count属性。有没有更好的方法来做到这一点?我希望将其保留为表单集合,这样我就可以向表单添加一些自定义验证约束。请帮忙!我将在下面添加一些代码,以便澄清。

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';
    }
}

1 个答案:

答案 0 :(得分:1)

如果有任何按钮点击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)
        }
    }