如何在表单中列出实体

时间:2016-09-07 13:00:28

标签: symfony

我有一个实体Hall,可以欢迎另一个hall(次级)或stand

我坚持使用hall表单(或者至少我认为需要编辑的表单)。 我无法弄清楚如何让<select>显示所有hall parent=0 我还需要一个默认选项,该选项将为空白,0为值

以下是我的文件:

霍尔实体:

<?php

namespace SalonBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * Hall
 *
 * @ORM\Table(name="hall")
 * @ORM\Entity(repositoryClass="SalonBundle\Repository\HallRepository")
 */
class Hall {
 /**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
 private $idHall;

 /**
 * @var string
 * @ORM\Column(name="name", type="string", length=255)
 */
 private $name;

    /**
     * @var integer
     * @ORM\Column(name="parent", type="integer", options={"default":0})
     */
    private $parent;

    /**
     * @var
     * @ORM\OneToMany(targetEntity="SalonBundle\Entity\Stand", mappedBy="hall")
     */
    private $stand;

 public function __construct()
 {
 $this->stand = new \Doctrine\Common\Collections\ArrayCollection();
 }

 /**
 * Get idHall
 * @return integer
 */
 public function getIdHall()
 {
 return $this->idHall;
 }

 /**
 * Set name
 * @param string $name
 * @return Hall
 */
 public function setName($name)
 {
 $this->name = $name;

 return $this;
 }

 /**
 * Get name
 * @return string
 */
 public function getName()
 {
 return $this->name;
 }

    /**
     * Set parent
     * @param integer $parent
     * @return Hall
     */
    public function setParent($parent)
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * Get parent
     * @return string
     */
    public function getParent()
    {
        return $this->parent;
    }

 /**
 * Add stand
 * @param \SalonBundle\Entity\Stand $stand
 * @return Hall
 */
 public function addStand(\SalonBundle\Entity\Stand $stand)
 {
 $this->stand[] = $stand;

 return $this;
 }

 /**
 * Remove stand
 * @param \SalonBundle\Entity\Stand $stand
 */
 public function removeStand(\SalonBundle\Entity\Stand $stand)
 {
 $this->stand->removeElement($stand);
 }

 /**
 * Get stand
 * @return \Doctrine\Common\Collections\Collection
 */
 public function getStand()
 {
 return $this->stand;
 }

    /**
     * toString
     * @return string
     */
    public function __toString() {
        return $this->getName();
    }
}

霍尔表格:

<?php

namespace SalonBundle\Form;

use SalonBundle\Entity\Hall;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class HallType extends AbstractType
{
 /**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
 public function buildForm(FormBuilderInterface $builder, array $options)
 {
 $builder
 ->add('name')
 ->add('parent') // Need to complete this part
 ;
 }

 /**
 * @param OptionsResolver $resolver
 */
 public function configureOptions(OptionsResolver $resolver)
 {
 $resolver->setDefaults(array(
 'data_class' => 'SalonBundle\Entity\Hall'
 ));
 }
}

我该怎么办?

2 个答案:

答案 0 :(得分:3)

使用&quot; query_builder&#39;您可以过滤实体的选项:

http://symfony.com/doc/current/reference/forms/types/entity.html#ref-form-entity-query-builder

$builder->add('users', EntityType::class, array(
  'class' => 'AppBundle:User',
  'query_builder' => function (EntityRepository $er) {
      return $er->createQueryBuilder('u')
        ->orderBy('u.username', 'ASC');
  },
  'choice_label' => 'username',
));

答案 1 :(得分:2)

我猜你使用Doctrine,所以试试EntityType

use Symfony\Bridge\Doctrine\Form\Type\EntityType;

    $builder->add('nom')
        ->add('description')
        ->add('parent', EntityType::class, [
            'class' => 'SalonBundle:Hall',
            'choice_label' => 'nom',
            'choice_value' => 'idHall'
        ])
        ->add([...]);

}