学说2.1:联接部分中的嵌套查询



我遇到了很多麻烦来构建一个学说查询(在Symfony Project中),我刚刚完成了,它奏效了,但我想获得您的建议,我不确定我的解决方案是最好的。

使它变得简单,我有3个SQL表: 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,但是该语言不支持内部和加入部分的嵌套查询。

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

<?php
public function getWorkshopWithAllContent()
{
    $rsm = new ResultSetMappingBuilder($this->_em);
    $rsm->addRootEntityFromClassMetadata('FONWorkshopBundleEntityWorkshop', 'w', array(
        'id' => 'wId'
    ));
    $rsm->addJoinedEntityFromClassMetadata('FONWorkshopBundleEntityNews', 'n', 'w', 'news', array(
        'id' => 'nId',
        'title' => 'nTitle',
        'content' => 'nContent',
        'pubDatetime' => 'nPubDatetime',
        'validated' => 'nValidated',
        'workshop_id' => 'nWorkshop_id'
    ));
    $rsm->addJoinedEntityFromClassMetadata('FONWorkshopBundleEntityComment', '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('FONWorkshopBundleEntityWorkshop', 'w');
    $rsm->addJoinedEntityFromClassMetadata('FONWorkshopBundleEntityNews', 'n', 'w', 'news');
    $rsm->addJoinedEntityFromClassMetadata('FONWorkshopBundleEntityComment', '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.

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

解决此问题的永久方法是重命名表的所有列,但是我想知道是否有更轻松的解决方案。

谢谢。

学说的联接界面是自然的,但它在引擎盖下处理得很多,因此仅仅试图扩展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();
}

dotcerine手柄匹配您在SQL中明确指定的ID。检查http://docs.doctrine-project.org/en/2.0.x/reference/query-builder.html以获取更多方法来完善查询。

相关内容

  • 没有找到相关文章

最新更新