簡體   English   中英

我如何從樹枝中的學說中查看數據庫結果

[英]How can i view Result of Database from doctrine in 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進行查看。

我的錯誤在哪里?

首先,在你的控制器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 %} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM