我想创建一个像这样的下拉列表
<select id="valueImportMatchs">
<option id="5" value="id|image|name|reference|category|price_tex|price_tin|condition|no">test23</option>
<option id="6" value="id|image|name|reference|category|price_tex|price_tin|condition|no">test235</option>
<option id="7" value="id|image|supplier|reference|category|price_tex|price_tin|condition|no">t</option>
</select>
所以我尝试使用ChoiceType表单字段类型 我成功生成了带有选项id和选择标签的列表框,但从未使用选项值
这是我的formType代码
public function buildForm(FormBuilderInterface $builder, array $options){
$idSite = $this->session->get('_defaultWebSite')->getId();
$em = $this->em;
$data = array();
$importsMach = $em->getRepository('ImportCsvBundle:ImportMatch')->findBy(array('idSite' => (int) $idSite));
if(count($importsMach))
foreach ($importsMach as $key => $importMatch)
$data[$importMatch->getMatching()] = $importMatch;
$builder
->add('nameList', ChoiceType::class, [
'choices' => $data,
'choice_label' => function($importMatch, $key, $index) {
/** @var Category $category */
return strtoupper($importMatch->getName());
},
'choice_attr' => function($importMatch, $key, $index) {
return ['id' => strtolower($importMatch->getId())];
},
'choice_value' => function($importMatch) {
return $importMatch->getMatching();
},
'choices_as_values' => true,
]);
}
这是我的实体ImportMatch.php内容
class ImportMatch
{
public function __construct()
{
$this::$nameList[] = $this;
}
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var int
*
* @ORM\Column(name="id_site", type="integer")
*/
private $idSite;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=60)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="matching", type="string", length=255)
*/
private $matching;
/**
* @var array
*
* @ORM\Column(name="name_list", type="array")
*/
private static $nameList;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set idSite
*
* @param integer $idSite
*
* @return ImportMatch
*/
public function setIdSite($idSite)
{
$this->idSite = $idSite;
return $this;
}
/**
* Get idSite
*
* @return int
*/
public function getIdSite()
{
return $this->idSite;
}
/**
* Set name
*
* @param string $name
*
* @return ImportMatch
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set matching
*
* @param string $matching
*
* @return ImportMatch
*/
public function setMatching($matching)
{
$this->matching = $matching;
return $this;
}
/**
* Get matching
*
* @return string
*/
public function getMatching()
{
return $this->matching;
}
/**
* Set nameList
*
* @param array $nameList
*
* @return ImportMatch
*/
public function setNameList($nameList)
{
$this::$nameList = $nameList;
return $this;
}
/**
* Get nameList
*
* @return array
*/
public function getNameList()
{
return $this::$nameList;
}
}
编辑:我收到此错误
Attempted to call an undefined method named "getMatching" of class "Doctrine\Common\Collections\ArrayCollection".
在这一行
return $importMatch->getMatching();
提前致谢
答案 0 :(得分:0)
在{ }
(!!!)之后,您缺少大括号if
:
它应该是:
if(count($importsMach)) {
foreach ($importsMach as $key => $importMatch)
...
]);
}
编辑:谢谢tarek,我更新了我的答案
答案 1 :(得分:0)
我使用查询构建器
解决了我的问题 ->add('nameList', EntityType::class, [
'class'=>'ImportCsvBundle:ImportMatch',
'choice_label' => function($importMatch, $key, $index) {
return strtoupper($importMatch->getName());
},
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('im')
->where('im.idSite = :idSite')
->orderBy('im.id', 'ASC')
->setParameter('idSite', $this->session->get('_defaultWebSite')->getId());
},
'choice_attr' => function($importMatch, $key, $index) {
return ['id' => strtolower($importMatch->getId())];
},
'choice_value' => function($importMatch) {
return $importMatch->getMatching();
},
'choices_as_values' => false,
])