【问题标题】:how to create virtual column on Doctrine Symfony2如何在 Doctrine Symfony2 上创建虚拟列
【发布时间】:2015-02-12 06:47:30
【问题描述】:

如何在 Doctrine Symfony2 上创建虚拟专栏

我有两张桌子

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 %}

我可以添加对公司实体类别的查询吗? 请给我一个线索, 谢谢 最好的祝福, 兰帕克

【问题讨论】:

    标签: symfony doctrine


    【解决方案1】:

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

    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 就会为您处理一切。

    【讨论】:

    • 我尝试扩展 Twig Extension 并使用 Doctrine Repository 及其作品,谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-10-23
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 2015-02-27
    • 2015-09-11
    相关资源
    最近更新 更多