【发布时间】:2016-03-20 12:27:24
【问题描述】:
我想创建一个这样的下拉菜单
<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 和选项标签的列表框,但从未用于选项值
这是我的表单类型代码
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();
提前致谢
【问题讨论】:
标签: php forms symfony dropdown