Tomek Buszewski
Tomek Buszewski

Reputation: 7955

Symfony2 - can't display values from an entity

I'm finding (using findOneBy) a singe row in my entity. Here's the code:

$userown = $this->getDoctrine()->getRepository('GameShelfUsersBundle:Own')
    ->findOneBy(array(
        'game' => $game->getId(),
        'user' => $em->getRepository('GameShelfUsersBundle:User')->find($session->getId())
    ));

Now I pass it to template as userown. But when I try to print it in twig, using {{ userown.typo }}, it throws an error:

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Proxies\__CG__\GameShelf\UsersBundle\Entity\OwnState could not be converted to string in D:\!!XAMPP\htdocs\

My Entity is here.

Upvotes: 1

Views: 3494

Answers (2)

Mirage
Mirage

Reputation: 31568

Are you sure this is correct

'game' => $game->getId()

I think it should be the game object itself rather than the id

'game' => $game,

Upvotes: 1

Sgoettschkes
Sgoettschkes

Reputation: 13214

Doctrine automatically resolves your foreign keys, so $typo is not a string but an object. As the error message tells you, this object cannot be converted to a string, so printing it fails.

You can either implement the __toString() method inside your OwnState Entity (it should return a string) or you print an actual property of the OwnState object: {{ userown.type.someProperty }}.

Upvotes: 5

Related Questions