2015-11-21 21 views
0

Image of the form collectionSymfony的更新表單收集來自內部控制器

我一直在現在正在尋找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'; 
    } 
} 
+0

好吧,如果我沒有理解對不對,「投票」 buttuon應綁定到onclick事件的JavaScript,而不是提交。因爲在這裏提交而不是任何意義。所以onclick處理程序應該將投票計數加1。 – felipsmartins

+0

感謝您的幫助,但我需要一個不需要javascript的解決方案,因爲我只想用Symfony和PHP來構建這個解決方案。 –

+0

好吧,只要記住,並不總是這是可能的。 ;) – felipsmartins

回答

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) 
     } 
    } 
+0

非常感謝。這是正確的答案! –

+0

還有一個問題給你Ziumin。如果用戶投票選擇特定的每月零食,那麼我將每月零食存入cookie中。我試圖添加一個自定義驗證約束到Vote類,它將從requestStack對象獲取cookie,如果每月點心已經被投票,它會拋出一個錯誤。設置自定義約束類很容易,但我該如何去獲取自定義驗證器類中的對象投票?每個投票對象都被傳遞到驗證器中,我不確定如何找出驗證器類中的哪個被點擊的fromm。 –

+0

或者也許你有一個更好的方法來做到這一點,建議讓我知道。謝謝Ziumin。 –