2015-06-20 80 views
0

編輯:改變findOneBy找到BY 在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: "[email protected]" 
#emailCanonical: "[email protected]" 
#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查看。

我的錯誤在哪裏?

+2

是不是idUser應該是唯一的?因爲findOneBy應該總是返回一個結果。 –

+0

你是如何循環瀏覽結果的?你有嘗試過這樣的嗎? {%爲idcustomer%的客戶} {{customer.surename}} { %ENDFOR%} –

+0

我得到這個錯誤:變速 「idCustomer」 不存在FPMAllgemeinBundle:開始:companydata.html.twig在第11 –

回答

2

首先,在你的控制器findBy()總是會返回一個數組,因此,如果您更換customerUsercustomerUsers你的代碼會更清楚:

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