Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从数据库中获取值并将其插入到下拉列表Symfony 3中_Php_Symfony - Fatal编程技术网

Php 从数据库中获取值并将其插入到下拉列表Symfony 3中

Php 从数据库中获取值并将其插入到下拉列表Symfony 3中,php,symfony,Php,Symfony,您好,我尝试将数据库中的数据插入下拉列表或选择类型。数据来自两个不同的数据库 这是我的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->c

您好,我尝试将数据库中的数据插入下拉列表或选择类型。数据来自两个不同的数据库

这是我的
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';
    }


}

首先,您需要根据返回的查询构建一个数组,其中包含国家名称:

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

    }
    $form = $this->createForm(CountriesType::class,$data);
然后您需要将options数组参数传递到add语句中。
只需替换此行:

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

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

抱歉,什么都没有…我的html:看看我的解决方案,如果你遇到一些问题,请告诉我我做了一些改进看看没有必要使用ArrayCollectionHappy来帮助y。你@Pablo Malynovytch是否可以接受我的回答,如果你认为它解决了你的问题,或者是最有助于找到你的解决方案。非常感谢。干杯谢谢你的回答,但我的实体没有getName作为函数,我尝试放置getID,但什么都没有…只显示未知信息,如标签格式、引用、额外字段、消息、csrf令牌、管理器。我尝试了,但没有你说的“FooChoices从代码而不是数据库中带来数据”您能告诉我您从数据库中带来的数据名是什么吗?(实体中的列名或属性名)我在数据库Translationtext中有列文本(我要打印的)和Countries表…那里我只有翻译的ID和ID(我要在select中请求作为值)…告诉我你是否理解或需要翻译的实体。查询是正确的,因为我可以将其打印为HTML。
//IndexController
$country = $query->getQuery()->getResult();
    $data= array();
     foreach ( $country as $c) {
        array_push($data,$c->getTranslation());

    }
    $form = $this->createForm(CountriesType::class,$data);
     //CountriesType
   ->add('countries',ChoiceType::class,['choices' => $this->fooChoices,])
 ->add('countries',ChoiceType::class,array('choices' => $options))