如何从树枝中的学说查看数据库结果

时间:2015-06-20 17:45:53

标签: php symfony doctrine-orm twig

编辑:将findOneBy更改为findBy 在symfony中我使用的是FOS-UserBundle。我有三张桌子。

fos_user 顾客 customer_user

这是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>

这是ActionController

/**
 * @Route("/companydata", name="fpm_companydata")
 */
public function companyDataAction(request $request)
{
    $user = $this->get('security.token_storage')->getToken()->getUser();
    $userId = $user->getId();

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

    return $this->render( "FPMAllgemeinBundle:Start:companydata.html.twig",
        array(
            "customerUser" => $customerUser
        )
    );
}

当我把它扔到树枝上时,我得到了以下结果:

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

如果我在自定义文件中只有一个结果,我可以查看例如带

的名字
{{ idCustomer.surename }}

但是当有超过一个结果匹配时,我得到了转储,我无法用foreach查看它。

我的错误在哪里?

1 个答案:

答案 0 :(得分:2)

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

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

return $this->render( "FPMAllgemeinBundle:Start:companydata.html.twig",
    array(
        "customerUsers" => $customerUsers
    )
);

然后在您的树枝中,您可以显示每个customerUsers的客户详细信息:

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