user2632918
user2632918

Reputation:

How can i view Result of Database from doctrine in twig

Edit: change findOneBy to findBy In symfony I am using the FOS-UserBundle. I have three tables.

fos_user customer customer_user

This is the 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>

This is the 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
        )
    );
}

And when i dump it in the twig, i got the following result:

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

When i have only one result in the customertable, i can view e.g. the firstname with

{{ idCustomer.surename }}

But when there ist more then one result matched, i got the dump above and i cant view it with a foreach.

Where is my mistake?

Upvotes: 0

Views: 504

Answers (1)

redbirdo
redbirdo

Reputation: 4957

Firstly, the findBy() in your controller will always return an array, so your code will be clearer if you replace customerUser with customerUsers:

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

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

Then in your twig you could display the customer details of each of the customerUsers like this:

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

Upvotes: 1

Related Questions