Symfony 2 添加动态表单;来自数据库的值
Symfony 2 add dynamic form; values from database
我想创建一个 google 类别匹配(来自数据库的第一个字段类别和第二个字段来自 google 类别的用户自动完成字段)表单,其中我有一个实体 CategoriesConfig :
private $id;
/**
* @var string
*
* @ORM\Column(name="category_site", type="string", length=100)
*/
private $categorySite;
/**
* @var string
*
* @ORM\Column(name="category_google", type="string", length=100)
*/
private $categoryGoogle;
在我的控制器中,我尝试了这个
/**
* @Route("/adminDashboard/categoriesMatching", name="googleShopping_categories")
* @Security("has_role('ROLE_SUPER_ADMIN')")
*/
public function categoriesMatchingAction(Request $request)
{
// create a task and give it some dummy data for this example
$idSite = $this->get('session')->get('_defaultWebSite')->getId();
$categories = $this->getDoctrine()->getRepository('DataSiteBundle:SiteCategory')->findBy(array('IdSite' => $idSite));;
$categories_config = new CategoriesConfig();
//var_dump($categories);exit;
$form = $this->createForm(new CategoriesConfigType($categories), $categories_config);
return $this->render('GoogleShoppingBundle:Default:categoriesMatching.html.twig', array(
'form' => $form->createView()
));
}
我的表单类型:CategoriesConfigType:
class CategoriesConfigType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
private $site_categories;
public function __construct ($site_categories) {
$this->site_categories = $site_categories;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
foreach($this->site_categories as $k => $categorie){
$builder
->add('categorySite')
->add('categoryGoogle');
}
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Sp\GoogleShoppingBundle\Entity\CategoriesConfig'
));
}
}
我希望类别行与行字段一样多(网站 itecategorie 和 google 类别)
结果是这样的:
提前致谢!
你在$this->categories
上的循环是无效的,因为你每次添加的元素都具有相同的名称(categorySite
和categoryGoogle
),所以FormBuilder每次都会替换表单字段,而不是再添加一个。
但是,如果您希望表单处理 CategoryConfig 集合,则需要采用不同的方法。
1) 创建一个 CategoriesConfigType
(如您所做),但谁只负责 一个 CategoriesConfig 实体
class CategoriesConfigType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('categorySite')
->add('categoryGoogle');
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Sp\GoogleShoppingBundle\Entity\CategoriesConfig'
));
}
}
2) 然后使用 CollectionType field 将您的表单作为整个 CategoryConfigTypes 集合进行操作:
class YourCollectionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('categoriesConfigs', CollectionType::class, array(
'entry_type' => CategoriesConfigType::class,
'entry_options' => array('required' => false)
);
}
}
我想创建一个 google 类别匹配(来自数据库的第一个字段类别和第二个字段来自 google 类别的用户自动完成字段)表单,其中我有一个实体 CategoriesConfig :
private $id;
/**
* @var string
*
* @ORM\Column(name="category_site", type="string", length=100)
*/
private $categorySite;
/**
* @var string
*
* @ORM\Column(name="category_google", type="string", length=100)
*/
private $categoryGoogle;
在我的控制器中,我尝试了这个
/**
* @Route("/adminDashboard/categoriesMatching", name="googleShopping_categories")
* @Security("has_role('ROLE_SUPER_ADMIN')")
*/
public function categoriesMatchingAction(Request $request)
{
// create a task and give it some dummy data for this example
$idSite = $this->get('session')->get('_defaultWebSite')->getId();
$categories = $this->getDoctrine()->getRepository('DataSiteBundle:SiteCategory')->findBy(array('IdSite' => $idSite));;
$categories_config = new CategoriesConfig();
//var_dump($categories);exit;
$form = $this->createForm(new CategoriesConfigType($categories), $categories_config);
return $this->render('GoogleShoppingBundle:Default:categoriesMatching.html.twig', array(
'form' => $form->createView()
));
}
我的表单类型:CategoriesConfigType:
class CategoriesConfigType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
private $site_categories;
public function __construct ($site_categories) {
$this->site_categories = $site_categories;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
foreach($this->site_categories as $k => $categorie){
$builder
->add('categorySite')
->add('categoryGoogle');
}
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Sp\GoogleShoppingBundle\Entity\CategoriesConfig'
));
}
}
我希望类别行与行字段一样多(网站 itecategorie 和 google 类别)
结果是这样的:
提前致谢!
你在$this->categories
上的循环是无效的,因为你每次添加的元素都具有相同的名称(categorySite
和categoryGoogle
),所以FormBuilder每次都会替换表单字段,而不是再添加一个。
但是,如果您希望表单处理 CategoryConfig 集合,则需要采用不同的方法。
1) 创建一个 CategoriesConfigType
(如您所做),但谁只负责 一个 CategoriesConfig 实体
class CategoriesConfigType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('categorySite')
->add('categoryGoogle');
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Sp\GoogleShoppingBundle\Entity\CategoriesConfig'
));
}
}
2) 然后使用 CollectionType field 将您的表单作为整个 CategoryConfigTypes 集合进行操作:
class YourCollectionType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('categoriesConfigs', CollectionType::class, array(
'entry_type' => CategoriesConfigType::class,
'entry_options' => array('required' => false)
);
}
}