Field的名称表

时间:2017-08-25 03:06:46

标签: symfony doctrine-orm

我试图显示要在表头中使用的数据库中的字段名称,以显示在管理部分建议的树枝中!

我有像这样的数据库

id | Gram | Height | Kilos |

1  | 27.1 | 126 cm | 29 kg |

我希望获得字段名称“Gram,Height etc”并将其作为表格标题,我打算为英语翻译成日语等等,“问题是如何获得数据字段名称,并将其显示为文本的任何建议“,提前谢谢!

控制器代码

 /**
     * @Route("/ingredients/header-translation", name = "z_recipe_header_translation")
     * @Template("NutritionMainBundle:Admin\create\recipe\ingredients:translate-headers.html.twig")
     */
    public function headerTranslationAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $nutrients = $em->getRepository("NutritionAdminBundle:NutritionValue")->findAll();
        return array(
            'header' => $nutrients
        );
    }

Twig代码

<table class="table" style="width:100%;">
        <thead>
            <tr>
                <th>header</th>
                {% for ii in 1..9 %}
                <th>language {{ii}}</th>
                {% endfor %}
            </tr>
        </thead>
        <tbody>
            {% for i in 1..10 %}
            <tr>
                <td>header label {{i}}</td>
                {% for ii in 1..9 %}
                    <td><input type="text"/></td>
                {% endfor %}                
            </tr>
            {% endfor %}
        </tbody>
    </table>

哦,这是一个重复的问题,很抱歉,我只是以此为例,下面的代码将是我的最终答案,谢谢你的帮助!

控制器代码:

/**
     * @Route("/ingredients/header-translation", name = "z_recipe_header_translation")
     * @Template("NutritionMainBundle:Admin\create\recipe\ingredients:translate-headers.html.twig")
     */
    public function headerTranslationAction(Request $request)
    {
        $em = $this->getDoctrine()->getManager();
        $NutritionValueField = $em->getClassMetadata('NutritionAdminBundle:NutritionValue')->getFieldNames();

        $languages = $em->getRepository("NutritionLanguageBundle:Language")->findAll();

        $form = $this->createForm(new NutritionValueType());
        $form->handleRequest($request);

        return array(
            'header' => $NutritionValueField,
            'languages' => $languages,
            'form' => $form->createView()
        );
    }

我的Twigs显示

 <table class="table" style="width:100%;">
        <thead>
            <tr>
                <th>header</th>
                {% for languages in languages %}
                <th>{{languages.langLabel}}</th>
                {% endfor %}
            </tr>
        </thead>
        <tbody>
            {% for header in header %}
            <tr>
                {% if  header == 'NDB_NO'%}
                    {#do nothing#}
                {% else %}
                <td>{{ header|replace({'_':' '}) }}</td>
                {% for languages in languages %}
                    <td><input id="{{header}}_{{ languages.id }}" type="text" value="{{header|replace({'_':' '})}}"/> </td>
                {% endfor %}
                    {% endif %}
            </tr>
            {% endfor %}
        </t

体&GT;     

2 个答案:

答案 0 :(得分:0)

您可以使用数据库信息架构:

SELECT `COLUMN_NAME` 
FROM `INFORMATION_SCHEMA`.`COLUMNS` 
WHERE `TABLE_SCHEMA` = 'databasename' 
AND `TABLE_NAME` = 'tablename';

答案 1 :(得分:-1)

使用sql语句:desc tablename;