简体   繁体   English

在树枝Symfony3中的for循环内获取数据

[英]Fetching data inside a for loop in twig Symfony3

I'm beginner in Symfony, How do I fetch the value of sub_agent.companyId in my company table on views using symfony? 我是Symfony的初学者,如何使用symfony在视图的company表中获取sub_agent.companyId的值?

Controller: 控制器:

public function indexAction(Request $request) {

    $em = $this->getDoctrine()->getManager();

    $sub_agents = $this->getDoctrine()
        ->getRepository('AppBundle:Sub_agent')
        ->findAll();

    return $this->render('Backend/Sub_agent/index.html.twig', array(
        'sub_agents' => $sub_agents
    ));
}

Template: index.html.twig 模板:index.html.twig

{% for sub_agent in sub_agents %}

{{ sub_agent.companyId }} // Get the data of this companyId in my company table

{% endfor %}

Try this: 尝试这个:

{% for sub_agent in sub_agents %}
 {{ sub_agent.companyId|e }}
{% endfor %}

refer : http://twig.sensiolabs.org/doc/2.x/tags/for.html#iterating-over-keys 请参阅: http : //twig.sensiolabs.org/doc/2.x/tags/for.html#iterating-over-keys

I think the company has an relation with the sub_agent entity or not? 我认为公司与sub_agent实体有关系吗?

If it is a seperated entity you should first get the company and after that get the id of the company entity 如果是独立实体,则应首先获取公司,然后获取公司实体的ID

try this: 尝试这个:

{% for sub_agent in sub_agents %}
   {{ sub_agent.company.id }} 
{% endfor %}

Use your getter. 用你的吸气剂。 Not sure what it is called for your Entity. 不确定为您的实体调用什么。 Something like this: 像这样:

{% for sub_agent in sub_agents %}
   {{ sub_agent.getId }}
{% endfor %}

It should be as easy as that, since you normally can't call the attribute directly, so just use your Entity setters/getters to get/set them. 那样应该就这么简单,因为您通常无法直接调用该属性,因此只需使用Entity setters / getters来获取/设置它们。

EDIT #2 - Based on you didn't mention you had a separate Entity called Company 编辑#2-根据您的说法,您没有提到一个名为Company的单独实体

{% for sub_agent in sub_agents %}
   {{ sub_agent.getCompany.getId }}
{% endfor %}

The above presumes that the sub_agent Entity has a $company attribute and the getter is called getCompany() . 上面假设sub_agent实体具有$company属性,而该getter称为getCompany() Then getCompany() returns a Company object, and then the Company method getId() returns the Company ID. 然后getCompany()返回Company对象,然后Company方法getId()返回Company ID。 You could also use DanBuit's suggestion, but both Chi-Chi and my suggestions are the "typical" way to do it in Twig. 您也可以使用DanBuit的建议,但是Chi-Chi和我的建议都是在Twig中“典型”的做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM