Search code examples
phpformssymfonyformcollection

Get each Object id in Collection Form


I have a Collection of Colle Object in a form and I want to access id of each colle.

I tried :

$colles = $data['colles'];

Dump of $colles :

array (size=2)
  1 => 
object(PACES\ColleBundle\Entity\Colle)[4156]
  protected 'id' => null
  protected 'nom' => 
    object(PACES\ColleBundle\Entity\ColleQC)[4126]
      private 'questions' => 
        object(Doctrine\ORM\PersistentCollection)[4646]
          ...
      protected 'id' => int 140
      protected 'coefficient' => string '1.00' (length=4)

  protected 'coefficient' => int 1
  2 => 
object(PACES\ColleBundle\Entity\Colle)[4144]
  protected 'id' => null
  protected 'nom' => 
    object(PACES\ColleBundle\Entity\ColleQC)[4583]
      private 'questions' => 
        object(Doctrine\ORM\PersistentCollection)[4592]
          ...
      protected 'id' => int 150
      protected 'coefficient' => string '1.00' (length=4)
  protected 'coefficient' => int 1

For 1st object, I want getId() to get 'id' = 140 and for 2nd, 'id' = 150

This code returns null :

foreach ($colles as $colle) {

   $idColle = $colle->getId();
}

Solution

  • If you look at the dump of $colles, you see that the 'id' of both 1 and 2 are "null", but it is the 'nom' collection (I think that's the collection) that has the Ids you are looking for.

    Did you try:

    $idColleNom = $colle->getNom()->getId();
    

    I'm not certain of your setters and getters, but it might be something like that.