在我的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正如您所看到的,我有一个用于$ensembles的关系$ensembles和一个用于$typesactivite的ManyToMany关系。
我有这个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项目(带有Doctrine的DQL)中导入自己的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关系。我在<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>但我有个错误:
语义错误第0行,第328行靠近“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 ));
}但是现在发生了这个错误:
在呈现模板(“可捕获致命错误:类Doctrine\ORM\PersistentCollection的对象无法转换为C:\wamp\www.........\app\cache\dev\twig\bf\66\146a31a62bf6f2a549d2604fb5be9c4530ab760a10169af08e8a5b72e9ee.php第127行中的字符串”)的第60行时抛出了一个异常。
就像你看到的,在我的树枝上,一切都是正确的。有人吗?
谢谢你的帮助。
发布于 2015-01-28 19:45:26
似乎我找到了解决方案,多亏了另一个开发人员(顺便再次感谢您)。
看看这里:事实上,你必须在你的树枝上做一个循环。
因为您的代码中应该是这样的:
<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>希望它能帮到你。
发布于 2015-01-26 14:23:26
试一试(用@Gregsparrow的建议):
$qb = $this-> $em->getRepository("YourBundle:Batiments")
->createQueryBuilder('b');
// @Gregsparrows suggestion queryBuilder
$qb ->select("b")
->from("Batiments", "b")
->leftJoin(...")
->leftJoin("...");
$batiment = $qb->getResult();
return $this->render('...') ;它匹配吗?
发布于 2015-01-26 17:13:53
不对
$batiment = $em->getRepository('MySpaceDatabaseBundle:Batiments');
$qb = $this->$em->createQueryBuilder(b);正确的
$qb = $em->getRepository('MySpaceDatabaseBundle:Batiments')->createQueryBuilder('b');https://stackoverflow.com/questions/28150527
复制相似问题