Php 如何在细枝中查看来自条令的数据库结果

Php 如何在细枝中查看来自条令的数据库结果,php,symfony,doctrine-orm,twig,Php,Symfony,Doctrine Orm,Twig,编辑:将findOneBy更改为findBy 在symfony中,我使用FOS用户包。我有三张桌子 fos_用户 顾客 用户 这是customerUser.orm.xml文件 <?xml version="1.0" encoding="utf-8"?> <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/20

编辑:将findOneBy更改为findBy 在symfony中,我使用FOS用户包。我有三张桌子

fos_用户 顾客 用户

这是customerUser.orm.xml文件

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  <entity name="FPM\AppBundle\Entity\CustomerUser" table="customer_user">
    <indexes>
      <index name="customer_user_customer_fk1_idx" columns="id_customer"/>
      <index name="customer_user_user_fk2_idx" columns="id_user"/>
    </indexes>
    <id name="id" type="integer" column="id">
      <generator strategy="IDENTITY"/>
    </id>
    <many-to-one field="idUser" target-entity="User">
      <join-columns>
        <join-column name="id_user" referenced-column-name="id"/>
      </join-columns>
    </many-to-one>
    <many-to-one field="idCustomer" target-entity="Customer">
      <join-columns>
        <join-column name="id_customer" referenced-column-name="id"/>
      </join-columns>
    </many-to-one>
  </entity>
</doctrine-mapping>
当我把它倒在树枝上时,我得到了以下结果:

array:2 [▼
0 => CustomerUser {#1055 ▼
-id: 1
-idUser: User {#946 ▶}
-idCustomer: Customer {#1082 ▼
+__isInitialized__: false
-firstname: null
-surename: null
-companyname: null
-street: null
-zipcode: null
-city: null
-phone: null
-homepage: null
-email: null
-verified: null
-id: 1
-idCountry: null
…2
}
}
1 => CustomerUser {#1081 ▼
-id: 2
-idUser: User {#946 ▼
#id: 1
#username: "rol4nd"
#usernameCanonical: "rol4nd"
#email: "info@xxx.xx"
#emailCanonical: "info@xxx.xx"
#enabled: true
#salt: "cgadwc4up9484okkc8wc"
#password: "xnkOiX1kD/akMxkXLl9U2OYPyeKlvgQfN79GytQ=="
#plainPassword: null
#lastLogin: DateTime {#944 ▶}
#confirmationToken: null
#passwordRequestedAt: null
#groups: null
#locked: false
#expired: false
#expiresAt: null
#roles: []
#credentialsExpired: false
#credentialsExpireAt: null
}
-idCustomer: Customer {#1080 ▼
+__isInitialized__: false
-firstname: null
-surename: null
-companyname: null
-street: null
-zipcode: null
-city: null
-phone: null
-homepage: null
-email: null
-verified: null
-id: 2
-idCountry: null
…2
}
}
]
当customertable中只有一个结果时,我可以使用

{{ idCustomer.surename }}
但是当有超过一个匹配的结果时,我得到了上面的转储,我不能用foreach查看它

我的错误在哪里?

首先,控制器中的findBy()将始终返回一个数组,因此如果将
customerUser
替换为
customerUser
,代码将更清晰:

$customerUsers = $this->getDoctrine()
    ->getRepository('FPMAppBundle:CustomerUser')
    ->findBy(
        array( "idUser" => $userId )
    );

return $this->render( "FPMAllgemeinBundle:Start:companydata.html.twig",
    array(
        "customerUsers" => $customerUsers
    )
);
然后在您的小树枝中,您可以显示每个
客户的客户详细信息,如下所示:

{% for customerUser in customerUsers %} 
    Name: {{ customerUser.idCustomer.firstname }} {{ customerUser.idCustomer.surename }} 
    Company: {{ customerUser.idCustomer.companyname }}
{% endfor %} 

idUser不应该是唯一的吗?因为findOneBy应该总是只返回一个结果。你是如何循环返回结果的?你试过这样的吗?{%for idcustomer%}{{customer.surename}{%endfor%}我得到了这个错误:变量“idcustomer”在FPMAllgemeinBundle:Start:companydata.html.twig的第11行不存在,我想我得到了所有与该用户相关的客户,客户的“surename”属性应该是“姓氏”吗?
{% for customerUser in customerUsers %} 
    Name: {{ customerUser.idCustomer.firstname }} {{ customerUser.idCustomer.surename }} 
    Company: {{ customerUser.idCustomer.companyname }}
{% endfor %}