Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Database 如何创建从mysql数据库提取数据并在Symfony 4中构建表单的动态函数?_Database_Forms_Symfony_Entity_Formbuilder - Fatal编程技术网

Database 如何创建从mysql数据库提取数据并在Symfony 4中构建表单的动态函数?

Database 如何创建从mysql数据库提取数据并在Symfony 4中构建表单的动态函数?,database,forms,symfony,entity,formbuilder,Database,Forms,Symfony,Entity,Formbuilder,我想构建一个控制器,从数据库加载数据,并从中构建一个表单,但这取决于slug public function form($slug, Request $request){ $EntityName = 'App\\Entity\\' . ucwords($slug); $item= $this->getDoctrine()->getRepository($EntityName)->find($id); $form = $this->createFormBu

我想构建一个控制器,从数据库加载数据,并从中构建一个表单,但这取决于slug

public function form($slug, Request $request){
   $EntityName = 'App\\Entity\\' . ucwords($slug);
   $item= $this->getDoctrine()->getRepository($EntityName)->find($id);
   $form = $this->createFormBuilder($item)
这意味着,如果slug是例如
products
,那么我想从我的数据库表`products构建一个表单,其中包含该表中的所有字段:

因此,在
products
中,我有一些字段,例如
id、name、color
。所以我需要的输出是

  ->add('id', TextType::class, array('attr' => array('class' => 'form-control')))
  ->add('name', TextType::class, array('attr' => array('class' => 'form-control')))
  ->add('color', TextType::class, array('attr' => array('class' => 'form-control')))
但是如果我的slug是例如
members
,并且在我的
members
表中,字段是
id、username、email
,则应构建包含此字段的表单:

 ->add('id', TextType::class, array('attr' => array('class' => 'form-control')))
  ->add('username', TextType::class, array('attr' => array('class' => 'form-control')))
  ->add('email', TextType::class, array('attr' => array('class' => 'form-control')))
我现在的问题是如何创建依赖于slug的字段。我想从对象
$item
中获取字段。这是对象
$item

 object(App\Entity\Members)#4788 (6) {
      ["id":"App\Entity\Members":private]=>
      int(9)
      ["username":"App\Entity\Members":private]=>
      string(13) "123"
      ["plainPassword":"App\Entity\Members":private]=>
      NULL
      ["password":"App\Entity\Members":private]=>
      string(0) ""
      ["email":"App\Entity\Members":private]=>
      string(7) "1@12.sw"
      ["isActive":"App\Entity\Members":private]=>
      bool(true)
    }
我试图使用
get\u object\u vars
,但这不适用于私有对象,所以我想这不是一个好的解决方案。我还尝试从对象创建一个数组
($array=(array)$item;)
,用foreach构建表单结构。但这似乎也不是正确的方法


您是否有过从数据库表动态加载数据并创建表单的函数的经验?我为每一个想法或建议感到高兴。

你可以试试这样的东西

$cmf = $em->getMetadataFactory();
$class = $cmf->getMetadataFor($entityName);

foreach ($class->fieldMappings as $fieldMapping) {
  echo $fieldMapping['fieldName'] . "\n";
}
这将根据条令的元数据模型为您的实体提供所有字段

查看更多信息,尤其是您将在那里获得的信息

你可以用像这样的东西

  • isNullable(字符串$fieldName)
    以确保某些“强制验证”
  • getIdentifierColumnNames()
    以排除所有标识符列(注意:这将返回列名而不是实体字段,但您可以轻松识别这些字段
  • gettypeofield
    获取某个字段是字符串还是数字的提示(再次允许更具体的表单属性)
  • 等等…只需检查文档并尝试一下

但是,通常要小心使用ClassMetadata,因为您的用例并不是真正的用途,但我们也将其用于类似的情况。

您想做什么?只是问一下,因为可能有一个更简单的解决方案。如果您的所有实体都是条令实体,您可以轻松使用ClassMetadata,其中包含您需要每个实体,请参阅文档:例如,使用
$class=$em->getMetadataFactory()->getMetadataFor('Entities\User'));
为什么要将所有实体属性定义为表单输入?这种需求的目的是什么?这是一个奇怪的问题case@LBA这可能就是我想要的…你能举个例子吗?那么我认为@LBA的解决方案是我测试代码的方法,但是我没有得到任何输出请确保使用正确的$entityName和$em设置为您的EntityManager-将无法查看您的代码。这正在工作。我添加了
$em=$this->getDoctrine()->getManager();
$EntityName='App\\Entity\\\\'。ucwords($slug)
结果是什么?当您硬编码一个实体时会发生什么,如
$entityName='App\\entity\\Members'
-请注意,我使用的是entityName而不是entityName您甚至可以签出
$classes=$cmf->getAllMetadata();
以获取所有类的元数据,仅用于测试目的