2017-05-12 26 views
3

こんにちはデータベースからドロップダウンリストまたはChoiceTypeにデータを挿入しようとしています。データは、2つの異なるデータベースから得られます。ここでデータベースから値を取得し、ドロップダウンリストに挿入するsymfony 3

IndexControllerです:

public function indexAction(Request $request){ 
$em = $this->getDoctrine()->getManager(); 
    //$city = new Cities();->select("c.id,c.active,c.code,t.text name") 
$query = $em->createQueryBuilder() 
     ->select("c") 
     ->from("DataBaseBundle:Countries","c") 
     ->innerJoin("DataBaseBundle:Translationtext","t","WITH","c.translation=t.translation"); 
$country = $query->getQuery()->getResult(); 
if (!$country){ 
     throw $this->createNotFoundException("Country not found"); 
} 
$form = $this->createForm(CountriesType::class,$country); 

if ($form->isValid()) { 
    $user = $form->getData(); 
    $em->persist($user); 
    $em->flush(); 
} 
return $this->render('ParametersBundle:Countries:index.html.twig', array(
      'form' => $form->createView(),)); 
    } 
} 

そして、私のフォームはCountriesTypeと呼ばれる:

<?php 

namespace ParametersBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\Extension\Core\Type\CheckboxType; 
use Symfony\Component\Form\Extension\Core\Type\TextType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolver; 
use Symfony\Component\Form\Extension\Core\Type\SubmitType; 
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; 

class CountriesType extends AbstractType 
{ 
    private $fooChoices = [ 
     0 => 'choice naught', 
     1 => 'choice one', 
     2 => 'choice deuce', 
    ]; 
    /** 
    * {@inheritdoc} 
    */ 
    public function buildForm(FormBuilderInterface $builder, array $options){ 
     $builder->add('active',CheckboxType::class) 
     ->add('countries',TextType::class) 
     ->add('countries',ChoiceType::class,['choices' => $this->fooChoices,]) 
     ->add('save', SubmitType::class, array('label' => 'Create Post')); 
    } 
    /** 
    * {@inheritdoc} 
    */ 
    public function configureOptions(OptionsResolver $resolver) 
    { 
     $resolver->setDefaults(array(
      'choices' => array(
       'm' => 'Male', 
       'f' => 'Female', 
      ) 
      //'data_class' => 'DataBaseBundle\Entity\Cities' 
     )); 
    } 

    /** 
    * {@inheritdoc} 
    */ 
    public function getBlockPrefix() 
    { 
     return 'country'; 
    } 


} 

FooChoicesコードからではなく、データベースからのデータをもたらします。

EDIT:そして、私のエンティティ(traslationがTraslationsと呼ばれる別のデータベースにある)

<?php 

namespace DataBaseBundle\Entity; 

/** 
* Countries 
*/ 
class Countries 
{ 
    /** 
    * @var integer 
    */ 
private $id; 

/** 
* @var integer 
*/ 
private $active = '1'; 

/** 
* @var string 
*/ 
private $code2; 

/** 
* @var string 
*/ 
private $code3; 

/** 
* @var \DataBaseBundle\Entity\Continents 
*/ 
private $continents; 

/** 
* @var \DataBaseBundle\Entity\Translation 
*/ 
private $translation; 


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

/** 
* Set active 
* 
* @param integer $active 
* 
* @return Countries 
*/ 
public function setActive($active) 
{ 
    $this->active = $active; 

    return $this; 
} 

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

/** 
* Set code2 
* 
* @param string $code2 
* 
* @return Countries 
*/ 
public function setCode2($code2) 
{ 
    $this->code2 = $code2; 

    return $this; 
} 

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

/** 
* Set code3 
* 
* @param string $code3 
* 
* @return Countries 
*/ 
public function setCode3($code3) 
{ 
    $this->code3 = $code3; 

    return $this; 
} 

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

/** 
* Set continents 
* 
* @param \DataBaseBundle\Entity\Continents $continents 
* 
* @return Countries 
*/ 
public function setContinents(\DataBaseBundle\Entity\Continents $continents = null) 
{ 
    $this->continents = $continents; 

    return $this; 
} 

/** 
* Get continents 
* 
* @return \DataBaseBundle\Entity\Continents 
*/ 
public function getContinents() 
{ 
    return $this->continents; 
} 

/** 
* Set translation 
* 
* @param \DataBaseBundle\Entity\Translation $translation 
* 
* @return Countries 
*/ 
public function setTranslation(\DataBaseBundle\Entity\Translation $translation = null) 
{ 
    $this->translation = $translation; 

    return $this; 
} 

/** 
* Get translation 
* 
* @return \DataBaseBundle\Entity\Translation 
*/ 
public function getTranslation() 
{ 


     return $this->translation; 
    } 
} 
+0

が私の解決策を見て、あなたはいくつかの問題に直面した場合、私に知らせ – ahmedbhs

+0

私はいくつかのenhancmentをした外観に使用する必要がかかりません:1、これはと

 //CountriesType ->add('countries',ChoiceType::class,['choices' => $this->fooChoices,]) 


はちょうどこの行を置き換えますArrayCollection – ahmedbhs

+0

yをお手伝いします。 – ahmedbhs

答えて

0
<?php 
    namespace ParametersBundle\Form;  
    use Symfony\Component\Form\AbstractType; 
    use Symfony\Component\Form\Extension\Core\Type\CheckboxType; 
    use Symfony\Component\Form\Extension\Core\Type\TextType; 
    use Symfony\Component\Form\FormBuilderInterface; 
    use Symfony\Component\OptionsResolver\OptionsResolver; 
    use Symfony\Component\Form\Extension\Core\Type\SubmitType; 
    use Symfony\Component\Form\Extension\Core\Type\ChoiceType; 

    class CountriesType extends AbstractType 
    { 

     /** 
     * {@inheritdoc} 
     */ 
     public function buildForm(FormBuilderInterface $builder, array $options){ 
      $fooChoices = $options['choices']; 

      $builder->add('active',CheckboxType::class) 
      ->add('countries',TextType::class) 
      ->add('countries',ChoiceType::class,['choices' => $fooChoices,]) 
      ->add('save', SubmitType::class, array('label' => 'Create Post')); 
     } 
     /** 
     * {@inheritdoc} 
     */ 
     public function configureOptions(OptionsResolver $resolver) 
     { 
      $resolver->setDefaults(array(
       'choices' => null, 
       ) 
       //'data_class' => 'DataBaseBundle\Entity\Cities' 
      )); 
     } 

     /** 
     * {@inheritdoc} 
     */ 
     public function getBlockPrefix() 
     { 
      return 'country'; 
     } 


    } 

そして最後に、コントローラにフォームを構築するために:

$form = $this->createForm(CountriesType::class,array('choices' => $country)); 
+0

申し訳ありませんが...私のhtml:

2

配列を作成する必要があります。クエリは、国の名前が含まれます。

//IndexController 
$country = $query->getQuery()->getResult(); 
    $data= array(); 
    foreach ($country as $c) { 
     array_push($data,$c->getTranslation()); 

    } 
    $form = $this->createForm(CountriesType::class,$data); 

を次に、あなたはなステートメントを追加オプション配列引数を渡す必要があります。

->add('countries',ChoiceType::class,array('choices' => $options)) 
+0

回答ありがとうございますが、Entityは関数としてgetNameを持っていませんでした。 .. label_format、by_reference、by_reference、extra_fields_message、csrf_token_managerのような未知の情報が表示されます –

+0

何か考えていますか?何もしません –

+0

「FooChoicesはデータベースからではなくコードからデータを取り込みます。 – ahmedbhs

関連する問題