如何在下拉列表中选择一个值?
是表单构建器类
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;
}
}
提前致谢。
答案 0 :(得分: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();
}