- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 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/
我继承了一个零文档的 Symfony/PHP 网络应用程序代码库。我是一名 nodejs 开发人员,因此您可以想象其中的乐趣......!无论如何,我要做到这一点,我的重点是能够在本地运行该项目。 这
将 Twig 更新为 > 1.7 后 代码 原因: Twig_Error_Runtime: An exception has been thrown during the compilation of
自从几天前(2015 年 11 月 30 日)发布了最新的 Symfony LTS 版本后,我就开始使用它了。不幸的是,我无法使用在 Symfony 2.7.7 中运行良好的相同代码生成具有写入操作的
我最近将一切正常的 Silex v1.3 项目迁移到了 Silex v2。我收到一个 Twig 错误,我不明白原因 Twig_Error_Runtime in Template.php line 59
尝试在 Symfony 2.1 中打开路由时出现此错误 我真的不知道去哪里检查。互联网上什么都没有。 非常感谢您的帮助: CRITICAL - Uncaught PHP Exception Twig_
我的 symfony 项目中有这个实体: /** * Batiments * * @ORM\Table(name="batiments") * @ORM\Entity * @ORM\Enti
我是一名优秀的程序员,十分优秀!