我尝试为Symfony应用程序创建一个新的FormType。我有一个ManyToOne字段:CategoryGrower。 问题是我无法显示选择字段。它似乎但仍然是空的。
<?php
namespace FermierMalin\Bundle\GrowerBundle\Entity;
/**
* @ORM\Entity (repositoryClass="FermierMalin\Bundle\GrowerBundle\Entity\GrowerRepository")
* @ORM\Table (name="grower")
*/
class Grower
{
/**
* @ORM\Id
* @ORM\Column (type="integer")
* @ORM\GeneratedValue (strategy="AUTO")
*/
private $id;
/**
* @Assert\NotBlank(message="Le nom de l'exploitation est requis")
* @Assert\Length(min=2, max=100, minMessage="Le nom de l'exploitation doit contenir au moins {{ limit }} caractères", maxMessage="Le nom de l'exploitation doit contenir moins de {{ limit }} caractères")
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @ORM\ManyToOne(targetEntity="CategoryGrower", inversedBy="growers")
* @ORM\JoinColumn(name="category_id", referencedColumnName="id")
**/
private $category;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Grower
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set category
*
* @param CategoryGrower $category
* @return Grower
*/
public function setCategory(CategoryGrower $category = null)
{
$this->category = $category;
return $this;
}
/**
* Get category
*
* @return CategoryGrower
*/
public function getCategory()
{
return $this->category;
}
}
<?php
namespace FermierMalin\Bundle\GrowerBundle\Entity;
/**
* @ORM\Entity (repositoryClass="FermierMalin\Bundle\GrowerBundle\Entity\CategoryGrowerRepository")
* @ORM\Table (name="category_grower")
*/
class CategoryGrower
{
public function __construct() {
$this->growers = new ArrayCollection();
}
function __toString() {
return $this->label;
}
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="label", type="string")
*/
private $label;
/**
* @ORM\Column(name="parent", type="string")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="Grower", mappedBy="category")
**/
private $growers;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set label
*
* @param string $label
* @return CategoryGrower
*/
public function setLabel($label)
{
$this->label = $label;
return $this;
}
/**
* Get label
*
* @return string
*/
public function getLabel()
{
return $this->label;
}
/**
* Set parent
*
* @param string $parent
* @return CategoryGrower
*/
public function setParent($parent)
{
$this->parent = $parent;
return $this;
}
/**
* Get parent
*
* @return string
*/
public function getParent()
{
return $this->parent;
}
/**
* Add growers
*
* @param Grower $growers
* @return CategoryGrower
*/
public function addGrower(Grower $growers)
{
$this->growers[] = $growers;
return $this;
}
/**
* Remove growers
*
* @param Grower $growers
*/
public function removeGrower(Grower $growers)
{
$this->growers->removeElement($growers);
}
/**
* Get growers
*
* @return Collection
*/
public function getGrowers()
{
return $this->growers;
}
}
<?php
namespace FermierMalin\Bundle\GrowerBundle\Form;
class GrowerType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text')
->add('category', 'entity', array(
'class' => 'FermierMalin\Bundle\GrowerBundle\Entity\CategoryGrower',
'group_by' => 'parent',
))
->add('Enregistrer', 'submit')
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'FermierMalin\Bundle\GrowerBundle\Entity\Grower'
));
}
/**
* @return string
*/
public function getName()
{
return 'fermiermalin_bundle_growerbundle_grower';
}
}
在我的控制器中:
$grower = new Grower();
$formGrower = $this->createForm(new GrowerType(), $grower);
return array(
'formGrower' => $formGrower->createView(),
);
在我看来:
<h2>Information</h2>
{{ form(formGrower) }}
但是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