gpt4 book ai didi

php - 从数据库中获取值(value)并将其插入下拉列表 Symfony 3

转载 作者:搜寻专家 更新时间:2023-10-31 21:00:33 25 4
gpt4 key购买 nike

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

这是我的 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 从代码而不是数据库中获取数据。

编辑:和我的实体(翻译在另一个名为 Translations 的数据库中)

    <?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;
}
}

最佳答案

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

//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))

关于php - 从数据库中获取值(value)并将其插入下拉列表 Symfony 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43943810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com