gpt4 book ai didi

Symfony/Doctrine - Twig_Error_Runtime : Catchable Fatal Error: Object of class Doctrine\ORM\PersistentCollection could not be converted to string

转载 作者:行者123 更新时间:2023-12-02 17:47:16 24 4
gpt4 key购买 nike

我的 symfony 项目中有这个实体:

/**
* 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

如您所见,我有一个关系 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 项目中导入我的 SQl 请求(带有 Doctrine 的 DQL)。

我在我的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 关系。我将结果显示在 <table>html.twig 中的标签如下:

<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>

但是我有这个错误:

[Semantical Error] line 0, col 328 near 'typesactivite': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.

<小时/>

最后更新

<小时/>

根据所有建议,我尝试根据学说引用文档和 Symfonybook 来做到这一点。这是删除查询请求后 Controller 中的代码:

$em=$this->getDoctrine()->getManager();
$batiment = $em->getRepository('MySpaceDatabaseBundle:Batiments')->findAll();

return $this->render('MySpaceGestionPatrimoinesBundle:Batiments:indexBatiments.html.twig', array('batiment' => $batiment ));
}

但是现在发生了这个错误:

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.

正如你所看到的,在我的 Twig 上,一切都很好。有人吗?

感谢您的帮助。

最佳答案

感谢另一位开发人员,我似乎找到了解决方案(顺便再次感谢您)。

看看here :事实上你必须在你的 Twig 上打一个圈。

因为您的代码中应该是这样的:

<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>

希望对您有帮助。

关于Symfony/Doctrine - Twig_Error_Runtime : Catchable Fatal Error: Object of class Doctrine\ORM\PersistentCollection could not be converted to string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28150527/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com