gpt4 book ai didi

join - 学说 2.1 : Nested queries inside a Join section

转载 作者:行者123 更新时间:2023-12-04 09:43:33 25 4
gpt4 key购买 nike

我在构造 Doctrine 查询时遇到了很多麻烦(在 Symfony 项目中),我刚刚完成它并且它有效但我想获得你的建议,我不确定我的解决方案是最好的。

为简单起见,我有 3 个 SQL 表:WorkshopNewsComment。他们中的最后两个与 Workshop 有 ManyToOne 关系。

我的目标是在一个页面上显示一个 Workshop 实体以及相关的 News 和 Comment 实体。为了使事情稍微复杂一些,我只需要显示最后 5 个新闻和评论实体。而且,当然,这应该只在一个查询中完成(作为故障排除,到目前为止我一直在使用延迟加载)。

我已经成功完成了这个本地查询:

SELECT *
FROM Workshop w
LEFT JOIN (
SELECT * FROM News ORDER BY id DESC LIMIT 0, 5
) n ON w.id = n.workshop_id
LEFT JOIN (
SELECT * FROM Comment ORDER BY id DESC LIMIT 0, 5
) c ON w.id = c.workshop_id
WHERE w.id = 1 -- Random ID as an exemple

在此之后,我想将其转换为 DQL,但这种语言不支持 FROM 和 JOIN 部分内的嵌套查询。

我最后的选择是使用 native 查询...一段时间后,我已经能够在我的 Workshop 存储库中获得此​​结果:

<?php

public function getWorkshopWithAllContent()
{
$rsm = new ResultSetMappingBuilder($this->_em);

$rsm->addRootEntityFromClassMetadata('FON\WorkshopBundle\Entity\Workshop', 'w', array(
'id' => 'wId'
));

$rsm->addJoinedEntityFromClassMetadata('FON\WorkshopBundle\Entity\News', 'n', 'w', 'news', array(
'id' => 'nId',
'title' => 'nTitle',
'content' => 'nContent',
'pubDatetime' => 'nPubDatetime',
'validated' => 'nValidated',
'workshop_id' => 'nWorkshop_id'
));

$rsm->addJoinedEntityFromClassMetadata('FON\WorkshopBundle\Entity\Comment', 'c', 'w', 'comments', array(
'id' => 'cId',
'title' => 'cTitle',
'content' => 'cContent',
'pubDatetime' => 'cPubDatetime',
'validated' => 'cValidated',
'workshop_id' => 'cWorkshop_id'
));

$query = $this->_em->createNativeQuery(
'SELECT '.
'w.id wId, w.town, w.description, w.zipcode, w.address, '.
'n.id nId, n.title nTitle, n.content nContent, n.pubDatetime nPubDatetime, n.validated nValidated, n.workshop_id nWorkshop_id, '.
'c.id cId, c.title cTitle, c.content cContent, c.pubDatetime cPubDatetime, c.validated cValidated, c.workshop_id cWorkshop_id '.
'FROM Workshop w '.
'LEFT JOIN ( '.
' SELECT * FROM News ORDER BY id DESC LIMIT 0, 5 '.
') n ON w.id = n.workshop_id '.
'LEFT JOIN ( '.
' SELECT * FROM Comment ORDER BY id DESC LIMIT 0, 5 '.
') c ON w.id = c.workshop_id '.
'WHERE w.id = 1 ',
$rsm
);

return $query->getOneOrNullResult();
}

那么,为什么要重新定义几乎所有的列呢?因为如果我不这样做,就像这样:

<?php

public function getWorkshopWithAllContent()
{
$rsm = new ResultSetMappingBuilder($this->_em);

$rsm->addRootEntityFromClassMetadata('FON\WorkshopBundle\Entity\Workshop', 'w');

$rsm->addJoinedEntityFromClassMetadata('FON\WorkshopBundle\Entity\News', 'n', 'w', 'news');

$rsm->addJoinedEntityFromClassMetadata('FON\WorkshopBundle\Entity\Comment', 'c', 'w', 'comments');

$query = $this->_em->createNativeQuery(
'SELECT * '.
'FROM Workshop w '.
'LEFT JOIN ( '.
' SELECT * FROM News ORDER BY id DESC LIMIT 0, 5 '.
') n ON w.id = n.workshop_id '.
'LEFT JOIN ( '.
' SELECT * FROM Comment ORDER BY id DESC LIMIT 0, 5 '.
') c ON w.id = c.workshop_id '.
'WHERE w.id = 1 ',
$rsm
);

return $query->getOneOrNullResult();
}

我得到这个错误:

The column 'id' conflicts with another column in the mapper.

就是这样!我现在的问题是:有更好的解决方案吗?我的真的很长,我很惊讶 Doctrine 没有想到解决方案。

解决此问题的永久方法是重命名表的所有列,但我想知道在执行此操作之前是否有更简单的解决方案。

谢谢。

最佳答案

Doctrine 的连接接口(interface)很自然,但它在幕后处理了相当多的事情,所以简单地尝试扩展 sql 有时会让你出错。你没有显示实体结构,所以我猜这个研讨会看起来像:

YAML(我的偏好):

Workshop:
type: entity
<< other fields >>
manyToOne:
news:
targetEntity: News
comment:
targetEntity: Comment

或等效的注释(必须包括因为大多数 stackoverflow 文章似乎都在注释中):

class Workshop {
//... << other fields >>
/**
* @ManyToOne(targetEntity="News")
* @JoinColumn(name="news_id", referencedColumnName="id")
*/
private $comment;
/**
* @ManyToOne(targetEntity="Comment")
* @JoinColumn(name="comment_id", referencedColumnName="id")
*/
private $comment;
//...
}

尝试:

public function getWorkshopWithAllContent($id){

$qb= $this->_em->createQueryBuilder('w');
$qb->leftJoin('w.news', 'n')
->leftJoin('w.comment', 'c')
->andWhere($qb->expr()->eq('w.id',$id));

return $qb->getQuery()->getResult();

}

Dotcrine 处理匹配您在 sql 中明确指定的 ID。检查http://docs.doctrine-project.org/en/2.0.x/reference/query-builder.html了解更多优化查询的方法。

关于join - 学说 2.1 : Nested queries inside a Join section,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13012769/

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