我一直在現在正在尋找4天,找不到有關這個網站上的任何內容。我有一個表格的選票集。在這上面是它看起來像的圖片。當用戶單擊「投票」按鈕時,我需要更新正上方的「計數」字段。我如何在控制器內執行此操作?當我按下「投票」按鈕時,整個表單將被提交,我不知道要增加哪個計數屬性。有沒有更好的方法來做到這一點?我想保留這個表單集合,這樣我可以添加一些自定義驗證約束到表單。請幫忙!我將在下面添加一些我的代碼,以供澄清。
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;
}
}
votes類型:
<?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';
}
}
vote類型:
<?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';
}
}
好吧,如果我沒有理解對不對,「投票」 buttuon應綁定到onclick事件的JavaScript,而不是提交。因爲在這裏提交而不是任何意義。所以onclick處理程序應該將投票計數加1。 – felipsmartins
感謝您的幫助,但我需要一個不需要javascript的解決方案,因爲我只想用Symfony和PHP來構建這個解決方案。 –
好吧,只要記住,並不總是這是可能的。 ;) – felipsmartins