drogon982007 2015-07-12 10:05
浏览 17
已采纳

如何在没有多余结果的情况下对一个对象进行部分保湿,并在有或没有DQL的情况下形成一对一的关系?

I'm trying to fetch basic data from my DB to display these into a slider, and i don't want to fetch all the data for optimisation purpose, so i've made a simple method inside my Entity Repository to handle this... but when i run the bellow method, i will have redundant results like document * image

public function getLastGuidesSamples()
{
    return $this->_em->createQuery("
        SELECT
            document.slug,
            document.title,
            document.description,
            image.source,
            image.alternative,
            image.width,
            image.height
        FROM AppBundle:Document document
        JOIN AppBundle:Image image
        WHERE document.type = 3
        AND document.isPublished = true
        ORDER BY document.addingDate
    ")->setMaxResults(5)->getResult();
}

e.g. if i have 10 documents, and for some reasons 17 images i will have a total of 170 results, and if i set the maximum results to 5, all the results will be the first document with 5 differents images (image 1, image 2, image 3, etc... until image 17).

So how do i partially hydrate an object without redundant results in a one-to-one relationship with or without DQL?

  • 写回答

1条回答 默认 最新

  • dongshan1396 2015-07-13 07:22
    关注

    the WITH condition was missing, the following function pay the bills :

    public function getLastGuidesSamples()
    {
        return $this->_em->createQuery("
            SELECT
                document.slug,
                document.title,
                document.description,
                document.addingDate,
                document.addingAuthor,
                image.source,
                image.alternative,
                image.width,
                image.height
            FROM AppBundle:Document document
            JOIN AppBundle:Image image
            WITH image.id = document.id
            WHERE document.type = 3
            AND document.isPublished = true
            ORDER BY document.addingDate
        ")->setMaxResults(5)->getResult();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法