2016-08-29 9 views
0

私は、エンティティエンティティを含む埋め込みフォームを参照するOrganizationエンティティを持っています。フォームは問題なく保存されますが、Country EntityTypeフィールドは編集時に値を正しく取得しません。したがって、正しいオプションではなく、ドロップダウンの最初のオプションが常に選択されます。Symfony [2.8.9]埋め込みフォーム内のエンティティタイプが値を選択しない

OrganisationType形式:

<?php 

namespace AppBundle\Form\Type\Meta; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\Form\FormEvent; 
use Symfony\Component\Form\FormEvents; 
use Symfony\Component\OptionsResolver\OptionsResolver; 

class OrganisationType extends AbstractType 
{ 

    public function __construct() 
    { 

    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('title', new TitleType(), 
       array('required' => true, 'label' => false)) 
      ->add('country', new CountryType(), array('label' => false)) 
      ->add('location', 'text', 
       array('required' => false, 'label' => 'City'));  
    } 

    public function getName() 
    { 
     return 'organisation'; 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'AppBundle\Entity\Organisation', 
      'csrf_protection' => true, 
      'csrf_field_name' => '_token', 
      // a unique key to help generate the secret token 
      'intention' => 'organisation', 
      'cascade_validation' => true 
     )); 
    } 

} 

CountryType形式:

<?php 

namespace AppBundle\Form\Type\Meta; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Bridge\Doctrine\Form\Type\EntityType; 
use Doctrine\ORM\EntityRepository; 

class CountryType extends AbstractType 
{ 

    public function __construct() 
    { 

    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder 
      ->add('id', EntityType::class, array(
       'label' => 'Country', 
       'class' => 'AppBundle:Country', 
       'choice_label' => 'name', 
       'placeholder' => 'Choose an option...', 
       'query_builder' => function (EntityRepository $repository) { 
        return $repository->createQueryBuilder('c')->orderBy('c.name', 'ASC'); 
       } 
      )); 

    } 

    public function getName() 
    { 
     return 'country'; 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'AppBundle\Entity\Country' 
     )); 
    } 

} 

組織エンティティ:

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Symfony\Component\Validator\Constraints as Assert; 
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; 
use Gedmo\Mapping\Annotation as Gedmo; 
use AppBundle\Model\OrganisationModel; 

/** 
* Organisation 
* 
* @ORM\Table(name="organisation", indexes={@ORM\Index(name="fk_meta_org_country_idx", columns={"country_id"}), @ORM\Index(name="fk_meta_org_subdivision_idx", columns={"country_subdivision_id"}), @ORM\Index(name="fk_meta_org_user_entered_idx", columns={"entered_by_user_id"}), @ORM\Index(name="fk_meta_org_user_updated_idx", columns={"updated_by_user_id"}), @ORM\Index(name="fk_meta_org_title_idx", columns={"title_id"})}) 
* @ORM\Entity(repositoryClass="AppBundle\Entity\OrganisationRepository") 
*/ 
class Organisation extends OrganisationModel 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="location", type="string", length=45, nullable=false) 
    */ 
    private $location; 

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

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="date_entered", type="datetime", nullable=false) 
    * @Gedmo\Timestampable(on="create") 
    */ 
    private $dateEntered; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="date_updated", type="datetime", nullable=false) 
    * @Gedmo\Timestampable(on="update") 
    */ 
    private $dateUpdated; 

    /** 
    * @var \AppBundle\Entity\Country 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Country") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="country_id", referencedColumnName="id") 
    * }) 
    */ 
    private $country; 

    /** 
    * @var \AppBundle\Entity\CountrySubdivision 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\CountrySubdivision") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="country_subdivision_id", referencedColumnName="id") 
    * }) 
    */ 
    private $countrySubdivision; 

    /** 
    * @var \AppBundle\Entity\Title 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Title", cascade="persist") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="title_id", referencedColumnName="id") 
    * }) 
    * @Assert\Type(type="AppBundle\Entity\Title") 
    * @Assert\Valid() 
    */ 
    private $title; 

    /** 
    * @var \AppBundle\Entity\User 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="entered_by_user_id", referencedColumnName="id") 
    * }) 
    */ 
    private $enteredByUser; 

    /** 
    * @var \AppBundle\Entity\User 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\User") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="updated_by_user_id", referencedColumnName="id") 
    * }) 
    */ 
    private $updatedByUser; 

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

    /** 
    * Set location 
    * 
    * @param string $location 
    * @return Organisation 
    */ 
    public function setLocation($location) 
    { 
     $this->location = $location; 

     return $this; 
    } 

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

    /** 
    * Set hidden 
    * 
    * @param boolean $hidden 
    * @return Organisation 
    */ 
    public function setHidden($hidden) 
    { 
     $this->hidden = $hidden; 

     return $this; 
    } 

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

    /** 
    * Set dateEntered 
    * 
    * @param \DateTime $dateEntered 
    * @return Organisation 
    */ 
    public function setDateEntered($dateEntered) 
    { 
     $this->dateEntered = $dateEntered; 

     return $this; 
    } 

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

    /** 
    * Set dateUpdated 
    * 
    * @param \DateTime $dateUpdated 
    * @return Organisation 
    */ 
    public function setDateUpdated($dateUpdated) 
    { 
     $this->dateUpdated = $dateUpdated; 

     return $this; 
    } 

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

    /** 
    * Set country 
    * 
    * @param \AppBundle\Entity\Country $country 
    * @return Organisation 
    */ 
    public function setCountry(\AppBundle\Entity\Country $country = null) 
    { 
     $this->country = $country; 

     return $this; 
    } 

    /** 
    * Get country 
    * 
    * @return \AppBundle\Entity\Country 
    */ 
    public function getCountry() 
    { 
     return $this->country; 
    } 

    /** 
    * Set countrySubdivision 
    * 
    * @param \AppBundle\Entity\CountrySubdivision $countrySubdivision 
    * @return Organisation 
    */ 
    public function setCountrySubdivision(\AppBundle\Entity\CountrySubdivision $countrySubdivision = null) 
    { 
     $this->countrySubdivision = $countrySubdivision; 

     return $this; 
    } 

    /** 
    * Get countrySubdivision 
    * 
    * @return \AppBundle\Entity\CountrySubdivision 
    */ 
    public function getCountrySubdivision() 
    { 
     return $this->countrySubdivision; 
    } 

    /** 
    * Set title 
    * 
    * @param \AppBundle\Entity\Title $title 
    * @return Organisation 
    */ 
    public function setTitle(\AppBundle\Entity\Title $title = null) 
    { 
     $this->title = $title; 

     return $this; 
    } 

    /** 
    * Get title 
    * 
    * @return \AppBundle\Entity\Title 
    */ 
    public function getTitle() 
    { 
     return $this->title; 
    } 

    /** 
    * Set enteredByUser 
    * 
    * @param \AppBundle\Entity\User $enteredByUser 
    * @return Organisation 
    */ 
    public function setEnteredByUser(\AppBundle\Entity\User $enteredByUser = null) 
    { 
     $this->enteredByUser = $enteredByUser; 

     return $this; 
    } 

    /** 
    * Get enteredByUser 
    * 
    * @return \AppBundle\Entity\User 
    */ 
    public function getEnteredByUser() 
    { 
     return $this->enteredByUser; 
    } 

    /** 
    * Set updatedByUser 
    * 
    * @param \AppBundle\Entity\User $updatedByUser 
    * @return Organisation 
    */ 
    public function setUpdatedByUser(\AppBundle\Entity\User $updatedByUser = null) 
    { 
     $this->updatedByUser = $updatedByUser; 

     return $this; 
    } 

    /** 
    * Get updatedByUser 
    * 
    * @return \AppBundle\Entity\User 
    */ 
    public function getUpdatedByUser() 
    { 
     return $this->updatedByUser; 
    } 
} 

国エンティティ:

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\Common\Collections\Criteria; 

/** 
* Country 
* 
* @ORM\Table(name="country", uniqueConstraints={@ORM\UniqueConstraint(name="unique", columns={"code"})}) 
* @ORM\Entity(repositoryClass="AppBundle\Entity\CountryRepository") 
*/ 
class Country 
{ 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="code", type="string", length=2, nullable=false) 
    */ 
    private $code; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="code_iso_3", type="string", length=3, nullable=true) 
    */ 
    private $codeIso3; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="code_iso_numeric", type="string", length=4, nullable=true) 
    */ 
    private $codeIsoNumeric; 

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

    /** 
    * @var string 
    * 
    * @ORM\Column(name="capital", type="string", length=30, nullable=true) 
    */ 
    private $capital; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="continent_name", type="string", length=15, nullable=true) 
    */ 
    private $continentName; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="continent_code", type="string", length=2, nullable=true) 
    */ 
    private $continentCode; 

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

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="date_entered", type="datetime", nullable=false) 
    */ 
    private $dateEntered; 

    /** 
    * @var \DateTime 
    * 
    * @ORM\Column(name="date_updated", type="datetime", nullable=false) 
    */ 
    private $dateUpdated; 

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

    /** 
    * @var Doctrine\Common\Collections\ArrayCollection 
    * 
    * @ORM\OneToMany(targetEntity="CountrySubdivision", mappedBy="country") 
    */ 
    private $subdivisions; 

    /* 
    * @var AppBundle\Entity\CountrySubdivision 
    * Stored purely for the purposes of form capture 
    */ 
    private $subdivision; 

    /** 
    * @var Doctrine\Common\Collections\ArrayCollection 
    * 
    * @ORM\OneToMany(targetEntity="CountrySubdivisionType", mappedBy="country") 
    */ 
    private $subdivisionTypes; 


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


    public function getSubdivisions() 
    { 
     return $this->subdivisions->toArray(); 
    } 

    public function getSubdivisionsOfParentId($parentId) 
    { 
     $criteria = Criteria::create() 
      ->where(Criteria::expr()->eq("parentId", $parentId)) 
      ->orderBy(array("name" => Criteria::ASC)); 

     return $this->subdivisions->matching($criteria)->toArray(); 
    } 

    public function setSubdivisions($subdivisions) 
    { 

    } 

    public function getSubdivision() 
    { 
     return $this->subdivision; 
    } 

    public function setSubdivision($subdivision) 
    { 
     $this->subdivision = $subdivision; 
    } 

    public function setId($id) 
    { 
     $this->id = $id; 
    } 

    public function getSubdivisionTypeOfParentId($parentId) 
    { 
     $criteria = Criteria::create() 
      ->where(Criteria::expr()->eq("subdivisionId", $parentId)) 
      ->orderBy(array("name" => Criteria::ASC)); 

     return $this->subdivisionTypes->matching($criteria)[0]; 
    } 


    /** 
    * Set code 
    * 
    * @param string $code 
    * @return Country 
    */ 
    public function setCode($code) 
    { 
     $this->code = $code; 

     return $this; 
    } 

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

    /** 
    * Set codeIso3 
    * 
    * @param string $codeIso3 
    * @return Country 
    */ 
    public function setCodeIso3($codeIso3) 
    { 
     $this->codeIso3 = $codeIso3; 

     return $this; 
    } 

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

    /** 
    * Set codeIsoNumeric 
    * 
    * @param string $codeIsoNumeric 
    * @return Country 
    */ 
    public function setCodeIsoNumeric($codeIsoNumeric) 
    { 
     $this->codeIsoNumeric = $codeIsoNumeric; 

     return $this; 
    } 

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

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

     return $this; 
    } 

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

    /** 
    * Set capital 
    * 
    * @param string $capital 
    * @return Country 
    */ 
    public function setCapital($capital) 
    { 
     $this->capital = $capital; 

     return $this; 
    } 

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

    /** 
    * Set continentName 
    * 
    * @param string $continentName 
    * @return Country 
    */ 
    public function setContinentName($continentName) 
    { 
     $this->continentName = $continentName; 

     return $this; 
    } 

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

    /** 
    * Set continentCode 
    * 
    * @param string $continentCode 
    * @return Country 
    */ 
    public function setContinentCode($continentCode) 
    { 
     $this->continentCode = $continentCode; 

     return $this; 
    } 

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

    /** 
    * Set hidden 
    * 
    * @param boolean $hidden 
    * @return Country 
    */ 
    public function setHidden($hidden) 
    { 
     $this->hidden = $hidden; 

     return $this; 
    } 

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

    /** 
    * Set dateEntered 
    * 
    * @param \DateTime $dateEntered 
    * @return Country 
    */ 
    public function setDateEntered($dateEntered) 
    { 
     $this->dateEntered = $dateEntered; 

     return $this; 
    } 

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

    /** 
    * Set dateUpdated 
    * 
    * @param \DateTime $dateUpdated 
    * @return Country 
    */ 
    public function setDateUpdated($dateUpdated) 
    { 
     $this->dateUpdated = $dateUpdated; 

     return $this; 
    } 

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

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

私は... CountryTypeの形で、

->add('name') 

...と

->add('id', EntityType::class, ... 

を交換した場合...私はどんな問題なく表示された国の名前を参照してください。 EntityTypeでどこが間違っていますか?

+0

<?php namespace AppBundle\Form\Type\Meta; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormEvent; use Symfony\Component\Form\FormEvents; use Symfony\Component\OptionsResolver\OptionsResolver; class OrganisationType extends AbstractType { public function __construct() { } public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('title', new TitleType(), array('required' => true, 'label' => false)) ->add('country', new CountryType(), array( 'label' => 'Country', 'choice_label' => 'name', 'placeholder' => 'Choose an option...', 'query_builder' => function (EntityRepository $repository) { return $repository->createQueryBuilder('c')->orderBy('c.name', 'ASC'); } )) ->add('location', 'text', array('required' => false, 'label' => 'City')); } public function getName() { return 'organisation'; } public function configureOptions(OptionsResolver $resolver) { $resolver->setDefaults(array( 'data_class' => 'AppBundle\Entity\Organisation', 'csrf_protection' => true, 'csrf_field_name' => '_token', // a unique key to help generate the secret token 'intention' => 'organisation', 'cascade_validation' => true )); } } 

あなたも '' OrganisationType' Country'の適切なコードを提供してもらえますか? –

+0

確かに。 OrganisationTypeがすでに存在していたため、エンティティの後ろにいると仮定しました。ありがとう。 – Graham

答えて

0

あなたはカスタムFormTypeを正しく使用していないと思います。カスタムFormTypeのビルダーに何も追加する必要はありません。これを試してみてください:

<?php 
namespace AppBundle\Form\Type\Meta; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Bridge\Doctrine\Form\Type\EntityType; 
use Doctrine\ORM\EntityRepository; 

class CountryType extends AbstractType 
{ 
    public function getName() 
    { 
     return 'country'; 
    } 

    public function getParent() 
    { 
     return EntityType::class; 
    } 

    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'data_class' => 'AppBundle\Entity\Country' 
     )); 
    } 
} 
+0

私はそれが行く方法だとは思わない。埋め込みフォームが役に立たなくなるでしょう。ドキュメントは次のとおりです。[link](https://symfony.com/doc/2.8/form/embedded.html) – Graham

+0

あなたの問題はフォームの埋め込みに関連していません。フォーム埋め込みは、互いに関連する異なるエンティティからプロパティを更新するためのものです。ここでは、Organizationエンティティを更新したいだけです。後で再利用できるカスタムフォームタイプを使用します。 (後で再利用する必要がない場合は、カスタムタイプなしでも使用できます) – VaN

+0

面白い、ありがとう。基本的には、CountryTypeを動的に(Statesなどのために)拡張したいので、埋め込みフォームのアプローチを使用しようとしていました。これは、別のフォームで再利用したいコードの塊です。現時点では、カスタムフォームがこのクリーナーの作成に役立つようには見えませんが、後で問題を再訪します。今のところ、私はOrganisationTypeにこのロジックを追加してCountryTypeを廃止しました。ご協力いただきありがとうございます。 – Graham

関連する問題