如何在 Doctrine Symfony2 上创建虚拟列

how to create virtual column on Doctrine Symfony2

如何在 Doctrine Symfony2 上创建虚拟列

我有两个table

table Company
---------------------------------
id  | company_name      | address
---------------------------------
1   | sample Co Ltd     | NY
2   | company Co Ltd    | LA
---------------------------------

table Ships
---------------------------------
id  | company_id    | ships_name
---------------------------------
1   | 1             | Ship ABC
1   | 1             | Ship XYZ
---------------------------------

如何在 symfony2 学说中使用虚拟列 (total_ships) 创建查询,以便我可以在 twig 模板中显示这样的数据:

{% for entity in pagination %}
   <tr>
    <td>{{ entity.company_name }}</a></td>
    <td>{{ entity.total_ships }}</td>
   </tr>
{% endfor %}

我可以添加对公司实体 Class 的查询吗? 请给我一个线索, 谢谢 此致, 拉姆帕克

您需要在实体中指定实体关系 类。

http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html

http://symfony.com/doc/current/book/doctrine.html#entity-relationships-associations

完成此操作并更新架构后,您可以在 Twig 中执行此操作:

{{ company.ships|length }}

统计一下。 您还可以执行任何类型的操作,例如

您需要做的就是将 Company 对象传递给 Twig,Twig + Doctrine 将为您处理所有事情。