2015-05-05 41 views
0

我在我的FormType下面的代碼:實體的選擇列表不發送值

/** 
* @var User 
*/ 
protected $currentUser; 

/** 
* param User $currentUser 
*/ 
public function __construct($currentUser) 
{ 
    $this->currentUser = $currentUser; 
} 

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $currentUser = $this->currentUser; 

    $builder 
     ->add('breedingPairMale', 'entity', array(
      'class' => 'Breedr\GeckoBundle\Entity\Gecko', 
      'property' => 'id', 
      'property' => 'name', 
      'placeholder' => 'Choose a Male', 
      'query_builder' => function(EntityRepository $er) use ($currentUser) { 
       return $er->createQueryBuilder('g') 
        ->where('g.user = :currentUser') 
        ->andWhere('g.sex = :sex') 
        ->orderBy('g.name') 
        ->setParameter('currentUser', $currentUser) 
        ->setParameter('sex', 'Male'); 
      } 
     )) 
     ->add('breedingPairFemale', 'entity', array(
      'class' => 'Breedr\GeckoBundle\Entity\Gecko', 
      'property' => 'id', 
      'property' => 'name', 
      'placeholder' => 'Choose a Female', 
      'query_builder' => function(EntityRepository $er) use ($currentUser) { 
       return $er->createQueryBuilder('g') 
        ->where('g.user = :currentUser') 
        ->andWhere('g.sex = :sex') 
        ->orderBy('g.name') 
        ->setParameter('currentUser', $currentUser) 
        ->setParameter('sex', 'Female'); 
      } 
     )) 
     ->add('breedingPairDate') 
     ->add('breedingPairSeason') 
    ; 
} 

它建立的選擇框精絕,當我檢查元素我看到價值是壁虎ID和名稱也是可見的,所以它都很好。

它提交表單正確太:

public function createAction(Request $request) 
{ 
    $entity = new Pairs(); 
    $form = $this->createCreateForm($entity); 
    $form->handleRequest($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($entity); 
     $em->flush(); 

     return $this->redirect($this->generateUrl('pairs_show', array('id' => $entity->getId()))); 
    } 

    return $this->render('BreedrBreedingPairsBundle:Pairs:new.html.twig', array(
     'entity' => $entity, 
     'form' => $form->createView(), 
    )); 
} 

/** 
* Creates a form to create a Pairs entity. 
* 
* @param Pairs $entity The entity 
* 
* @return \Symfony\Component\Form\Form The form 
*/ 
private function createCreateForm(Pairs $entity) 
{ 
    $currentUser = $this->getUser()->getId(); 

    $form = $this->createForm(new PairsType($currentUser), $entity, array(
     'action' => $this->generateUrl('pairs_create'), 
     'method' => 'POST', 
    )); 

    $form->add('submit', 'submit', array('label' => 'Create')); 

    return $form; 
} 

...然而,當它保存到數據庫中,對於男性和女性在數據庫表中的值是0

這是我idGecko類:

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

我是否需要爲一個ManyToOne關係這工作?還是我缺少別的東西?

由於提前,

安迪

編輯:

Pairs實體:

<?php 

namespace Breedr\BreedingPairsBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

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

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

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

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="breeding_pair_date", type="date") 
    */ 
    private $breedingPairDate; 

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


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

    /** 
    * Set id 
    * 
    * @return Pairs 
    */ 
    public function setId($id) 
    { 
     $this->id = $id; 

     return $this; 
    } 

    /** 
    * Set breedingPairMale 
    * 
    * @param integer $breedingPairMale 
    * @return Pairs 
    */ 
    public function setBreedingPairMale($breedingPairMale) 
    { 
     $this->breedingPairMale = $breedingPairMale; 

     return $this; 
    } 

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

    /** 
    * Set breedingPairFemale 
    * 
    * @param integer $breedingPairFemale 
    * @return Pairs 
    */ 
    public function setBreedingPairFemale($breedingPairFemale) 
    { 
     $this->breedingPairFemale = $breedingPairFemale; 

     return $this; 
    } 

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

    /** 
    * Set breedingPairDate 
    * 
    * @param \DateTime $breedingPairDate 
    * @return Pairs 
    */ 
    public function setBreedingPairDate($breedingPairDate) 
    { 
     $this->breedingPairDate = $breedingPairDate; 

     return $this; 
    } 

    /** 
    * Get breedingPairDate 
    * 
    * @return \DateTime 
    */ 
    public function getBreedingPairDate() 
    { 
     return $this->breedingPairDate; 
    } 

    /** 
    * Set breedingPairSeason 
    * 
    * @param integer $breedingPairSeason 
    * @return Pairs 
    */ 
    public function setBreedingPairSeason($breedingPairSeason) 
    { 
     $this->breedingPairSeason = $breedingPairSeason; 

     return $this; 
    } 

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

Gecko實體:

<?php 

namespace Breedr\GeckoBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 
use Symfony\Component\HttpFoundation\File\File; 
use Vich\UploaderBundle\Mapping\Annotation as Vich; 

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

    /** 
    * @var string 
    * 
    * @ORM\Column(name="name", type="string", length=255) 
    */ 
    private $name; 

    /** 
    * @ORM\OneToMany(targetEntity="Weight", mappedBy="geckoId", cascade={"persist", "remove"}) 
    */ 
    private $weights; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="aquisition_date", type="date") 
    */ 
    private $aquisitionDate; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="morph", type="string", length=255) 
    */ 
    private $morph; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="sex", type="string", length=64) 
    */ 
    private $sex; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="genetics", type="text") 
    */ 
    private $genetics; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="bio", type="text") 
    */ 
    private $bio; 

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

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

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

    /** 
    * NOTE: This is not a mapped field of entity metadata, just a simple property. 
    * 
    * @Vich\UploadableField(mapping="gecko_image", fileNameProperty="imageName") 
    * 
    * @var File $imageFile 
    */ 
    protected $imageFile; 

    /** 
    * @ORM\Column(type="string", length=255, name="image_name") 
    * 
    * @var string $imageName 
    */ 
    protected $imageName; 

    /** 
    * @ORM\Column(type="datetime") 
    * 
    * @var \DateTime $updatedAt 
    */ 
    protected $updatedAt; 

    /** 
    * @ORM\ManyToOne(targetEntity="Breedr\UserBundle\Entity\User", inversedBy="geckos") 
    * @ORM\JoinColumn(name="user_id", referencedColumnName="id") 
    */ 
    protected $user; 

    /** 
    * If manually uploading a file (i.e. not using Symfony Form) ensure an instance 
    * of 'UploadedFile' is injected into this setter to trigger the update. If this 
    * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter 
    * must be able to accept an instance of 'File' as the bundle will inject one here 
    * during Doctrine hydration. 
    * 
    * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image 
    */ 
    public function setImageFile(File $image = null) 
    { 
     $this->imageFile = $image; 

     if ($image) { 
      // It is required that at least one field changes if you are using doctrine 
      // otherwise the event listeners won't be called and the file is lost 
      $this->updatedAt = new \DateTime('now'); 
     } 
    } 

    /** 
    * @return File 
    */ 
    public function getImageFile() 
    { 
     return $this->imageFile; 
    } 

    /** 
    * @param string $imageName 
    */ 
    public function setImageName($imageName) 
    { 
     $this->imageName = $imageName; 
    } 

    /** 
    * @return string 
    */ 
    public function getImageName() 
    { 
     return $this->imageName; 
    } 

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

    public function __toString(){ 
     return $this->name; 
    } 

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

    /** 
    * Set name 
    * 
    * @param string $name 
    * @return Gecko 
    */ 
    public function setName($name) 
    { 
     $this->name = $name; 

     return $this; 
    } 

    /** 
    * Get name 
    * 
    * @return string 
    */ 
    public function getName() 
    { 
     return $this->name; 
    } 

    /** 
    * Set aquisitionDate 
    * 
    * @param \DateTime $aquisitionDate 
    * @return Gecko 
    */ 
    public function setAquisitionDate($aquisitionDate) 
    { 
     $this->aquisitionDate = $aquisitionDate; 

     return $this; 
    } 

    /** 
    * Get aquisitionDate 
    * 
    * @return \DateTime 
    */ 
    public function getAquisitionDate() 
    { 
     return $this->aquisitionDate; 
    } 

    /** 
    * Set morph 
    * 
    * @param string $morph 
    * @return Gecko 
    */ 
    public function setMorph($morph) 
    { 
     $this->morph = $morph; 

     return $this; 
    } 

    /** 
    * Get morph 
    * 
    * @return string 
    */ 
    public function getMorph() 
    { 
     return $this->morph; 
    } 

    /** 
    * Set sex 
    * 
    * @param string $sex 
    * @return Gecko 
    */ 
    public function setSex($sex) 
    { 
     $this->sex = $sex; 

     return $this; 
    } 

    /** 
    * Get sex 
    * 
    * @return string 
    */ 
    public function getSex() 
    { 
     return $this->sex; 
    } 

    /** 
    * Set genetics 
    * 
    * @param string $genetics 
    * @return Gecko 
    */ 
    public function setGenetics($genetics) 
    { 
     $this->genetics = $genetics; 

     return $this; 
    } 

    /** 
    * Get genetics 
    * 
    * @return string 
    */ 
    public function getGenetics() 
    { 
     return $this->genetics; 
    } 

    /** 
    * Set bio 
    * 
    * @param string $bio 
    * @return Gecko 
    */ 
    public function setBio($bio) 
    { 
     $this->bio = $bio; 

     return $this; 
    } 

    /** 
    * Get bio 
    * 
    * @return string 
    */ 
    public function getBio() 
    { 
     return $this->bio; 
    } 

    /** 
    * Set bred 
    * 
    * @param boolean $bred 
    * @return Gecko 
    */ 
    public function setBred($bred) 
    { 
     $this->bred = $bred; 

     return $this; 
    } 

    /** 
    * Get bred 
    * 
    * @return boolean 
    */ 
    public function getBred() 
    { 
     return $this->bred; 
    } 

    /** 
    * Set hatchling 
    * 
    * @param boolean $hatchling 
    * @return Gecko 
    */ 
    public function setHatchling($hatchling) 
    { 
     $this->hatchling = $hatchling; 

     return $this; 
    } 

    /** 
    * Get hatchling 
    * 
    * @return boolean 
    */ 
    public function getHatchling() 
    { 
     return $this->hatchling; 
    } 

    /** 
    * Set clutch 
    * 
    * @param integer $clutch 
    * @return Gecko 
    */ 
    public function setClutch($clutch) 
    { 
     $this->clutch = $clutch; 

     return $this; 
    } 

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

    /** 
    * Set photo 
    * 
    * @param string $photo 
    * @return Gecko 
    */ 
    public function setPhoto($photo) 
    { 
     $this->photo = $photo; 

     return $this; 
    } 

    /** 
    * Get photo 
    * 
    * @return string 
    */ 
    public function getPhoto() 
    { 
     return $this->photo; 
    } 


    /** 
    * Set updatedAt 
    * 
    * @param \DateTime $updatedAt 
    * @return Gecko 
    */ 
    public function setUpdatedAt($updatedAt) 
    { 
     $this->updatedAt = $updatedAt; 

     return $this; 
    } 

    /** 
    * Get updatedAt 
    * 
    * @return \DateTime 
    */ 
    public function getUpdatedAt() 
    { 
     return $this->updatedAt; 
    } 

    /** 
    * Add weights 
    * 
    * @param \Breedr\GeckoBundle\Entity\Weight $weights 
    * @return Gecko 
    */ 
    public function addWeight(\Breedr\GeckoBundle\Entity\Weight $weights) 
    { 
     $this->weights[] = $weights; 

     return $this; 
    } 

    /** 
    * Remove weights 
    * 
    * @param \Breedr\GeckoBundle\Entity\Weight $weights 
    */ 
    public function removeWeight(\Breedr\GeckoBundle\Entity\Weight $weights) 
    { 
     $this->weights->removeElement($weights); 
    } 

    /** 
    * Get weights 
    * 
    * @return \Doctrine\Common\Collections\Collection 
    */ 
    public function getWeights() 
    { 
     return $this->weights; 
    } 

    /** 
    * Set user 
    * 
    * @param \Breedr\UserBundle\Entity\User $user 
    * @return Gecko 
    */ 
    public function setUser(\Breedr\UserBundle\Entity\User $user = null) 
    { 
     $this->user = $user; 

     return $this; 
    } 

    /** 
    * Get user 
    * 
    * @return \Breedr\UserBundle\Entity\User 
    */ 
    public function getUser() 
    { 
     return $this->user; 
    } 
} 
+1

你能告訴我們你的'Gecko'和'Pairs'實體的完整代碼嗎? –

+0

我會盡快做@JovanPerovic,我只是不得不回頭工作:) –

+0

@JovanPerovic終於到處去添加它:) –

回答

1

好吧,我在這裏發生的是數據類型的不匹配。

  • 你的形式告訴數據類型是entity(的Gecko
  • 你的實體,具有@Column註解,告訴它的簡單類型Request的處理

後,一切似乎都找到 - Gecko實體具有已被分配到$breedingPairMale$breedingPairFemale。然而,當談到堅持時,我感到驚訝的是,它沒有中斷。我很好奇爲什麼。

要解決這個問題,你需要修復(或實際上,創建)的關係在您的Pairs實體:現在

class Pairs 
{ 
    // ................ 
    // ................ 
    // ................ 

    /** 
    * @var Breedr\GeckoBundle\Entity\Gecko 
    * 
    * @ORM\ManyToOne(targetEntity="Breedr\GeckoBundle\Entity\Gecko") 
    * @ORM\JoinColumn(name="breeding_pair_male", referencedColumnName="id") 
    */ 
    private $breedingPairMale; 

    /** 
    * @var Breedr\GeckoBundle\Entity\Gecko 
    * 
    * @ORM\ManyToOne(targetEntity="Breedr\GeckoBundle\Entity\Gecko") 
    * @ORM\JoinColumn(name="breeding_pair_female", referencedColumnName="id") 
    */ 
    private $breedingPairFemale; 

    // ................ 
    // ................ 
    // ................ 

$breedingPairMale$breedingPairFemale是沒有類型integer而是類Gecko和持續的更長它應該工作。

+0

我會快速瀏覽一下,2分鐘:) –

+0

好吧有一些錯誤。首先是「實體未找到。 500內部服務器錯誤 - EntityNotFoundException「 –

+0

而且」錯誤:方法代理\ __ CG __ \ Breedr \ GeckoBundle \ Entity \ Gecko :: __ toString()一定不會拋出異常「 –