Problème pour afficher les résultats d'une relation ManyToMany => Symfony/Doctrine - Twig_Error_Runtime
Bonjour à tous, j'ai un soucis avec le rendu de mes résultats suite à l'utilisation de la méthode findAll() dans mon controller.
j'ai deux entités Batiments.php et Typesactivite.php qui sont reliés par une relation ManyToMany. Jusque là rien de bien problématique. Si on raisonne en table, dans ma base de données SQl j'ai donc une table de pivot (batiments_typesactivite).
Et lorsque j'utilise la méthode findAll() sur l'entité correspondante, j'ai cette erreur sur ma page:
Citation:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Doctrine\ORM\PersistentCollection could not be converted to string in C:\wamp\www\Repository Bitbucket\repos\SymfonyGirusEnexgir\app\cache\dev\twig\bf\66\146a31a62bf6f2a549d2604fb5be9c4530ab760a10169af08e8a5b72e9ee.php line 120") in EnexgirGestionPatrimoinesBundle:Batiments:indexBatiments.html.twig at line 60.
J'avoue ne pas comprendre réellement ce qu'il se passe.
Voici le code pour Batiments.php :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
|
class Batiments
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="referenceBatiment", type="string", length=150, nullable=false)
*/
private $referencebatiment;
/**
* @var string
*
* @ORM\Column(name="nom", type="string", length=150, nullable=true)
*/
private $nom;
/**
* @var \Ensembles
*
* @ORM\ManyToOne(targetEntity="Ensembles")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="ensembles_id", referencedColumnName="id")
* })
*/
private $ensembles;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Typesactivite", inversedBy="batiments")
* @ORM\JoinTable(name="batiments_typesactivite",
* joinColumns={
* @ORM\JoinColumn(name="batiments_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="typesactivite_id", referencedColumnName="id")
* }
* )
*/
private $typesactivite;
/**
* Constructor
*/
public function __construct()
{
$this->typesactivite = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set referencebatiment
*
* @param string $referencebatiment
* @return Batiments
*/
public function setReferencebatiment($referencebatiment)
{
$this->referencebatiment = $referencebatiment;
return $this;
}
/**
* Get referencebatiment
*
* @return string
*/
public function getReferencebatiment()
{
return $this->referencebatiment;
}
/**
* Set nom
*
* @param string $nom
* @return Batiments
*/
public function setNom($nom)
{
$this->nom = $nom;
return $this;
}
/**
* Get nom
*
* @return string
*/
public function getNom()
{
return $this->nom;
}
/**
* Set ensembles
*
* @param \Enexgir\DatabaseBundle\Entity\Ensembles $ensembles
* @return Batiments
*/
public function setEnsembles(\Enexgir\DatabaseBundle\Entity\Ensembles $ensembles = null)
{
$this->ensembles = $ensembles;
return $this;
}
/**
* Get ensembles
*
* @return \Enexgir\DatabaseBundle\Entity\Ensembles
*/
public function getEnsembles()
{
return $this->ensembles;
}
/**
* Add typesactivite
*
* @param \Enexgir\DatabaseBundle\Entity\Typesactivite $typesactivite
* @return Batiments
*/
public function addTypesactivite(\Enexgir\DatabaseBundle\Entity\Typesactivite $typesactivite)
{
$this->typesactivite[] = $typesactivite;
return $this;
}
/**
* Remove typesactivite
*
* @param \Enexgir\DatabaseBundle\Entity\Typesactivite $typesactivite
*/
public function removeTypesactivite(\Enexgir\DatabaseBundle\Entity\Typesactivite $typesactivite)
{
$this->typesactivite->removeElement($typesactivite);
}
/**
* Get typesactivite
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getTypesactivite()
{
return $this->typesactivite;
}
public function __toString()
{
return $this->nom;
}
} |
J'ai suivi la doc de Doctrine pour permettre à cette relation ManyToMany d'avoir une entité "owner" donc Batiments.php ici.
Voici le code pour Typesactivite.php :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
class Typesactivite
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="type", type="string", length=150, nullable=false)
*/
private $type;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Batiments", mappedBy="typesactivite")
*/
private $batiments;
/**
* Constructor
*/
public function __construct()
{
$this->batiments = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set type
*
* @param string $type
* @return Typesactivite
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* Add batiments
*
* @param \Enexgir\DatabaseBundle\Entity\Batiments $batiments
* @return Typesactivite
*/
public function addBatiment(\Enexgir\DatabaseBundle\Entity\Batiments $batiments)
{
$this->batiments[] = $batiments;
return $this;
}
/**
* Remove batiments
*
* @param \Enexgir\DatabaseBundle\Entity\Batiments $batiments
*/
public function removeBatiment(\Enexgir\DatabaseBundle\Entity\Batiments $batiments)
{
$this->batiments->removeElement($batiments);
}
/**
* Get batiments
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getBatiments()
{
return $this->batiments;
}
public function __toString()
{
return $this->type;
}
} |
Ensuite dans mon controller (j'ai même essayer de faire des DQl etc etc etc) j'ai appelé la méthode findAll() sur l'entité Batiments.php, voici le code:
Code:
1 2 3 4 5 6 7 8
|
public function indexBatimentsAction() {
$em=$this->getDoctrine()->getManager();
$batiment = $em->getRepository('EnexgirDatabaseBundle:Batiments')->findAll();
return $this->render('EnexgirGestionPatrimoinesBundle:Batiments:indexBatiments.html.twig', array('batiment' => $batiment, 'typeactivite' => $typeactivite ));
} |
Et enfin, pour afficher le résultat dans un <table>, voici le code de ma twig indexBatiments.html.twig :
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
<tbody>
{% for batiments in batiment %}
<tr>
<td>{{ batiments.referencebatiment }}</td>
<td>{{ batiments.typesactivite }}</td>
<td>{{ batiments.ensembles.parcsimmobilier }}</td>
<td>{{ batiments.nom }}</td>
<td>{{ batiments.ensembles }}</td>
</tr>
{% endfor %}
</tbody> |
Lorsque je veux afficher ces résultats, l'erreur est telle que je l'ai donné plus haut:
Citation:
An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Doctrine\ORM\PersistentCollection could not be converted to string in C:\wamp\
http://www.........\app\cache\dev\tw...8a5b72e9ee.php line 127") in EnexgirGestionPatrimoinesBundle:Batiments:indexBatiments.html.twig at line 60.
Si l'un de vous peut m'éclairer, j'avoue ne plus savoir comment gérer ceci.
Merci d'avance.