-1

如何在下拉列表中选择一个值?

我有一个下拉列表,当我在编辑案例中填写下拉列表时,如何显示数据库中的选定值?

它是表单构建器类

 public function buildForm(FormBuilder $builder, array $options)
   { 
   $builder
        ->add('name')
        ->add('description')
        ->add('parentid', 'entity', array('class'=>'MusicCoreBundle:MusicCategory',
            'property'  => 'name',
            'required'  => false,
            'query_builder' => function(EntityRepository $er) {return $er->createQueryBuilder('s')->orderBy('s.name', 'ASC');},
            'empty_value' => 'No category'));

}

这是用于 ER,我认为与 id 和 parentid 的实体关系是错误的,请用代码回复,在实体声明中:

   /**
   * Music\Bundles\Core\Entity\MusicCategory
   * @ORM\Table(name="ms_musiccategory")
   * @ORM\Entity()
   * @ORM\Entity(repositoryClass="Music\Bundles\Core\Entity\Repository\CategoryRepository")
   * @UniqueEntity(fields={"name"},message="The name is already in this system.")
  */
  class MusicCategory 
  {
/**
     * @ORM\Column(name="id",type="integer")
     * @ORM\Id()
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

    /**
     *@ORM\ManyToOne(targetEntity="MusicCategory")
    *@ORM\JoinColumn(name="parent_Id", referencedColumnName="id")
     * 
     */
    private $parentid;
/**
* @ORM\Column(name="name", type="string", length=50, unique=true)
*/
private $name;

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


    public function __construct()
{
}

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

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

    /**
    * Set id
    *
    * @return integer
    */
    public function setId($id)
    {
       $this->id = $id;
    }

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

     /**
     * Get parentid
     *
     * @return integer 
     */
    public function setParentId($parentid)
     {   $this->parentid = $parentid;
     }
    /**
     * Set role
     *
     * @param string $role
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Set description
     *
     * @param string $description
     */
    public function setDescription($description)
    {
    $this->description = $description;
    }

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

   }

提前致谢。

4

1 回答 1

0

更改此代码如下:

只是改变实体类

   /**
     * @ORM\Column(name="id",type="integer")
     * @ORM\Id()
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

    /**
      * @ORM\OneToMany(targetEntity="MusicCategory", mappedBy="parent")
     */
    protected $children;

   /**
     * @ORM\ManyToOne(targetEntity="MusicCategory", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
    */
    protected $parent;

/**
* @ORM\Column(name="name", type="string", length=50, unique=true)
*/

   public function __construct()
{
        $this->parentId = null; // Default value for column parent_id
         $this->children = new \Doctrine\Common\Collections\ArrayCollection();
}
于 2012-12-11T10:37:31.880 回答