我尝试为Symfony应用程序创建一个新的FormType。我有一个ManyToOne字段:
// Grower.php
/**
* @ORM\ManyToOne(targetEntity="CategoryGrower", inversedBy="growers")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
**/
private $category;
// CategoryGrower.php
/**
* @ORM\OneToMany(targetEntity="Grower", mappedBy="category")
**/
private $growers;
// GrowerType.php
$builder
->add('name', 'text')
->add('category', 'entity', array(
'class' => 'FermierMalin\Bundle\GrowerBundle\Entity\CategoryGrower',
'choice_label' => 'label',
'group_by' => 'parent',
));
但是HTML代码中的select是空的:
<div>
<label for="fermiermalin_bundle_growerbundle_grower_category" class="required">Category</label>
<select id="fermiermalin_bundle_growerbundle_grower_category" name="fermiermalin_bundle_growerbundle_grower[category]" required="required">
</select>
</div>
在我的数据库中,我有一些数据:
ID, label, parent
1,Fruits,Maraichers
2,Légumes,Maraichers
3,Vin,Boissons
4,Jus,Boissons
答案 0 :(得分:1)
我这样做了我的
实体
/**
* @ORM\ManyToOne(targetEntity="FamebitBundle\Entity\Price")
* @ORM\JoinColumn(name="price_id", referencedColumnName="id")
*/
protected $price;
FormType
->add('price', EntityType::class, array(
'class' => 'FamebitBundle\Entity\Price',
'property' => 'label',
'choice_value' => 'id',
))
Twig view
{{ form_row(form.price) }}
这对我有用:)