【问题标题】:Add a value to dropdown in symfony2在 symfony2 的下拉列表中添加一个值
【发布时间】: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


    【解决方案1】:

    我使用查询生成器解决了我的问题

     ->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,        
    
            ])         
    

    【讨论】:

      【解决方案2】:

      { } 后面缺少花括号 if (!!!): 应该是:

      if(count($importsMach)) {
          foreach ($importsMach as $key => $importMatch)
              ...
          ]);
      }
      

      编辑:谢谢 tarek,我更新了我的答案

      【讨论】:

      • foreach 有一个孩子,不需要大括号
      猜你喜欢
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 2013-06-24
      • 1970-01-01
      • 2019-11-24
      • 2020-03-04
      • 1970-01-01
      相关资源
      最近更新 更多