1

So, I'm trying to display all of the comments belonging to a game, but for some reason, I always get this error:

Method "id" for object "Doctrine\ORM\PersistentCollection" does not exist in AppBundle:Game:view.html.twig at line 26 

The getCommentsForGame looks like this

public function getCommentsForGame($game) { $id = $game->getId(); $query = $this->createQueryBuilder('game') ->select( 'game.id', 'game.title', 'comments.id', 'comments.content' ) ->innerJoin('game.comments', 'comments') ->where('game.id = :id') ->setParameter('id', $id) ->getQuery(); return $query->getResult(); } 

And then the Comment Entity:

/** * Id. * * @ORM\Id * @ORM\Column( * type="integer", * nullable=false, * options={ * "unsigned" = true * } * ) * @ORM\GeneratedValue(strategy="IDENTITY") * * @var integer $id */ private $id; /** * Get id. * * @return integer */ public function getId() { return $this->id; } /** * Games array * * @ORM\ManyToOne(targetEntity="Game", inversedBy="games") * @ORM\JoinColumn(name="game_id", referencedColumnName="id") * ) * * @var \Doctrine\Common\Collections\ArrayCollection $games */ protected $games; 

And Game Entity:

/** * Comments array * * @ORM\OneToMany( * targetEntity="AppBundle\Entity\Comment", * mappedBy="games" * ) */ protected $comments; 

In Twig I'm using this:

 {{ game.comments.id }} 

Where's my error?

3
  • Does your Game Entity have an $id attribute? If yes, is it public? If no, is there a getter (getId()) for it? Commented Jun 26, 2016 at 9:47
  • What are you calling at line 26 in your AppBundle:Game:view.html.twig file? Commented Jun 26, 2016 at 9:48
  • Yes, I do have attribute $id and getter. I edited my post to show this and the twig. Commented Jun 26, 2016 at 9:54

2 Answers 2

3

game.comments is returning a Collection and a collection doesn't have an ID. You have to loop through the Collection and get the ID for each Comment:

{% for comment in game.comments %} {{ comment.id }} {% endfor %} 

Try using dump() on game.comments and game.comments[0] to see what I mean.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, it does solve the problem, but now, although the site doesn't return any error message, no comments are shown and the array taken from getCommentsForGame seems to be empty. Do you have any idea why this may be?
Have there already been comments made? :-) It might also have something to do with your query. I don't think the INNER JOIN is what you need. What is the reason you've written your own method for this? Doctrines standard repository has support for relations out of the box.
Sadly, yeah :D And yes, I do believe it has to be something with query. I'll work on it and I'll research this support for relations deeper, if you say that they will suffice. Thank you for your time :)
0

You can set strict_variables: false in the twig config This fixed my issue

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.