Symfony / Doctrine - Twig_Error_Runtime:Catchable致命错误:类Doctrine\ORM\PersistentCollection的对象无法转换为字符串 [英] Symfony/Doctrine - Twig_Error_Runtime: Catchable Fatal Error: Object of class Doctrine\ORM\PersistentCollection could not be converted to string
问题描述
/ **
* Batiments
*
* @ ORM\Table(name =batiments)
* @ ORM\Entity
* @ ORM\Entity(repositoryClass =MySpace\DatabaseBundle\Repository\ BatimentsRepository)
* /
class Batiments
{
/ **
* @var整数
*
* @ ORM\Column (name =id,type =integer,nullable = false)
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =IDENTITY)
*
private $ id;
/ **
* @var string
*
* @ ORM\Column(name =nom,type =string,length = 150 ,nullable = true)
* /
private $ nom;
/ **
* @ ORM\ManyToOne(targetEntity =MySpace\DatabaseBundle\Entity\Ensembles)
* @ ORM\JoinColumn(nullable = false)
* /
private $ ensemble;
/ **
* @ ORM\ManyToMany(targetEntity =MySpace\DatabaseBundle\Entity\Typesactivite)
* @ ORM\JoinTable(name = batiments_typesactivite,
* joinColumns = {@ ORM\JoinColumn(name =batiments_id,referencedColumnName =id,nullable = false)},
* inverseJoinColumns = {@ ORM\JoinColumn name =typesactivite_id,referencedColumnName =id,nullable = false)}
*)
* /
private $ typesactivite;
// getter和setter
如你所见,我有一个 ManyToOne
为 $ ensembles
和 ManyToMany
关系 $ typesactivite
。
我有这个SQL请求:
SELECT b.referenceBatiment,b.nom,e.nom,p.nom,b.surfaceChauffee,ta.type
FROM`batiments` b,`ensembles` e, `parcsimmobilier` p,`typesactivite` ta,`batiments_typesactivite` bta
WHERE b.ensembles_id = e.id
AND e.parcsimmobilier_id = p.id
AND b.id = bta.batiments_id
AND ta.id = bta.typesactivite_id
GROUP BY p.nom,e.nom,b.nom,ta.type
在 PhpMyAdmin 上,SQL请求工作得很好,所以我必须在我的Symfony项目(DQL with Doctrine)中导入我的SQl请求。
我在我的 controller.php 中尝试这样:
$ query = $ em-> createQuery('SELECT b
FROM MySpaceDatabaseBundle:Ensembles e,MySpaceDatabaseBundle:Typesactivite ta,MySpaceDatabaseBundle:Parcsimmobilier p,MySpaceDatabaseBundle:Batiments b
WHERE b.ensembles = e.id
AND b.typesactivite = ta.id');
它似乎工作,但只是为了ManyToOne关系。我在 html.twig 中的< table>
标签中显示结果,如下所示:
< table id =dataTablesBatimentsclass =table table-bordered table-hovercellspacing =0width =100%>
< thead>
< tr>
< th>Référence< / th>
< th> Parc< / th>
< th> Nom< / th>
< Ensemble< / th>
< th> Type d'activité< / th>
< th> Surface< / th>
< th>< / th>
< / tr>
< / thead>
< tbody>
{%for batiments in batiment%}
< tr>
< td> {{batiments.referencebatiment}}< / td>
< td> {{batiments.ensembles.parcsimmobilier}}< / td>
< td> {{batiments.nom}}< / td>
< td> {{batiments.ensembles}}< / td>
< td> {{batiments.typesactivite}}< / td>
< td> {{batiments.surfacechauffee}}< / td>
< td>< a href =>< button class =btn btn-warning btn-xs>修饰符< / button>< / a>< / td>
< / tr>
{%endfor%}
< / tbody>
< / table>
但是我有这个错误:
[语义错误]行0,col 328 near'typesactivite':错误:
无效的PathExpression。 StateFieldPathExpression或
SingleValuedAssociationField预期。
最近更新
根据学说参考文献和Symfonybook,我尝试这样做。删除查询请求后,我的控制器中的代码如下:
$ em = $ this-> getDoctrine() - > getManager();
$ batiment = $ em-> getRepository('MySpaceDatabaseBundle:Batiments') - > findAll();
return $ this-> render('MySpaceGestionPatrimoinesBundle:Batiments:indexBatiments.html.twig',array('batiment'=> $ batiment));
}
但是这个错误现在出现了:
渲染模板时抛出异常
(Catchable致命错误:类
的对象Doctrine\ORM\PersistentCollection不能转换为
中的字符串C:\wamp\www ......... \app\cache\dev\twig\bf\66\146a31a62bf6f2a549d2604fb5be9c4530ab760a10169af08e8a5b72e9ee.php
$ 127 $)
MySpaceGestionPatrimoinesBundle:Batiments:indexBatiments.html.twig
line 60。
感谢您的帮助。
像我找到解决方案感谢另一个开发人员(再次感谢你的方式)。
查看 here :其实你必须在你的树枝上做一个循环。
在代码中应该是这样的:
< tbody> ;
{%for batiments in batiment%}
< tr>
< td> {{batiments.referencebatiment}}< / td>
< td> {{...}}< / td>
<! - 你的循环在这里 - >
< td>
{%for batiments in batiments.typesactivite%}
{{batiments.type}}
{%endfor%}
< / td>
{%endfor%}
< / tbody>
希望它可以帮助你。
I have this entity in my symfony project:
/**
* Batiments
*
* @ORM\Table(name="batiments")
* @ORM\Entity
* @ORM\Entity(repositoryClass="MySpace\DatabaseBundle\Repository\BatimentsRepository")
*/
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="nom", type="string", length=150, nullable=true)
*/
private $nom;
/**
* @ORM\ManyToOne(targetEntity="MySpace\DatabaseBundle\Entity\Ensembles")
* @ORM\JoinColumn(nullable=false)
*/
private $ensembles;
/**
* @ORM\ManyToMany(targetEntity="MySpace\DatabaseBundle\Entity\Typesactivite")
* @ORM\JoinTable(name="batiments_typesactivite",
* joinColumns={@ORM\JoinColumn(name="batiments_id", referencedColumnName="id", nullable=false)},
* inverseJoinColumns={@ORM\JoinColumn(name="typesactivite_id", referencedColumnName="id", nullable=false)}
* )
*/
private $typesactivite;
//getters and setters
As you can see, I have a relation ManyToOne
for the $ensembles
and a ManyToMany
relation for $typesactivite
.
I have this SQL request:
SELECT b.referenceBatiment, b.nom, e.nom, p.nom, b.surfaceChauffee, ta.type
FROM `batiments` b, `ensembles` e, `parcsimmobilier` p, `typesactivite` ta, `batiments_typesactivite` bta
WHERE b.ensembles_id = e.id
AND e.parcsimmobilier_id = p.id
AND b.id = bta.batiments_id
AND ta.id = bta.typesactivite_id
GROUP BY p.nom, e.nom, b.nom, ta.type
On PhpMyAdmin the SQL request works very well, and so I have to import my SQl request in my Symfony Project (DQL with Doctrine).
I try this in my controller.php:
$query=$em->createQuery('SELECT b
FROM MySpaceDatabaseBundle:Ensembles e, MySpaceDatabaseBundle:Typesactivite ta, MySpaceDatabaseBundle:Parcsimmobilier p, MySpaceDatabaseBundle:Batiments b
WHERE b.ensembles = e.id
AND b.typesactivite = ta.id');
It seems to work but just for the ManyToOne relation. I display the result in a <table>
tag in html.twig like this:
<table id="dataTablesBatiments" class="table table-bordered table-hover" cellspacing="0" width="100%">
<thead>
<tr>
<th>Référence</th>
<th>Parc</th>
<th>Nom</th>
<th>Ensemble</th>
<th>Type d'activité</th>
<th>Surface</th>
<th></th>
</tr>
</thead>
<tbody>
{% for batiments in batiment %}
<tr>
<td>{{ batiments.referencebatiment }}</td>
<td>{{ batiments.ensembles.parcsimmobilier }}</td>
<td>{{ batiments.nom }}</td>
<td>{{ batiments.ensembles }}</td>
<td>{{ batiments.typesactivite }}</td>
<td>{{ batiments.surfacechauffee }}</td>
<td><a href=""><button class="btn btn-warning btn-xs">Modifier</button></a></td>
</tr>
{% endfor %}
</tbody>
</table>
but I have this error:
[Semantical Error] line 0, col 328 near 'typesactivite': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.
LAST UPDATE
With all the suggestion I try to do this according to doctrine reference documentation and Symfonybook. Here's the code in my controller after removing the query request:
$em=$this->getDoctrine()->getManager();
$batiment = $em->getRepository('MySpaceDatabaseBundle:Batiments')->findAll();
return $this->render('MySpaceGestionPatrimoinesBundle:Batiments:indexBatiments.html.twig', array('batiment' => $batiment ));
}
But this error occured now:
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.........\app\cache\dev\twig\bf\66\146a31a62bf6f2a549d2604fb5be9c4530ab760a10169af08e8a5b72e9ee.php line 127") in MySpaceGestionPatrimoinesBundle:Batiments:indexBatiments.html.twig at line 60.
Like you can see, in my twig, all is right. Someone?
Thank you for the help.
It seems like I found the solution thanks to another developper (thank you again by the way).
Look at here: in fact you have to make a loop in your twig.
For it should be something like this in your code:
<tbody>
{% for batiments in batiment %}
<tr>
<td>{{ batiments.referencebatiment }}</td>
<td>{{ ... }}</td>
<!-- your loop here -->
<td>
{% for batiments in batiments.typesactivite %}
{{ batiments.type }}
{% endfor %}
</td>
{% endfor %}
</tbody>
Hope It helps you.
这篇关于Symfony / Doctrine - Twig_Error_Runtime:Catchable致命错误:类Doctrine\ORM\PersistentCollection的对象无法转换为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!