在规则2中,如何将子查询结果添加到主查询结果的末尾?

$QB-主查询

$sub子查询

public function findPosts(): array
    {
        $ids = '777, 775, 767, 756, 752, 751, 732, 727, 725, 721, 717, 710, 702, 693, 678, 671, 662, 658, 639, 638, 617, 598, 579';
        $qb = $this->createQueryBuilder('article');
        $qb->where('article.visible = 1')
            ->andWhere('article.published_at <= :present_date')
            ->setParameter('present_date', date('Y-m-d H:i:s'))
            ->orderBy('article.id', 'DESC')
            ->select('article.id', 'article.title', 'article.slug', 'article.slots_type', 'article.skill', 'article.type', 'article.views', 'article.vk_shares', 'article.fb_shares')
            ->leftJoin('article.img', 'image')
            ->addSelect('image.filename AS img')
            ->setMaxResults(30);

        $sub = $this->createQueryBuilder('article');
        $sub->where('article.id IN ('.$ids.')')
            ->andWhere('article.visible = 1')
            ->orderBy('article.id', 'DESC')
            ->select('article.id', 'article.title', 'article.slug', 'article.slots_type', 'article.skill', 'article.type', 'article.views', 'article.vk_shares', 'article.fb_shares');
        return $qb->getQuery()->execute();
    }

我try 使用field函数,但没有得到预期的结果

推荐答案

The quickest way is to merge the results of both queries.
As an old user of symfony / doctrine :
First thing i would say. Do not make a function that does two different things as much as you can.
Second, i advise you to not use select() function if it does not impact performance to remove it. It will help you have a better sustainable code. I mean it is optional to use it.

如果我完全理解您的用法,那么第二个请求的结果必须位于主请求的末尾是必须的. 您可以执行UNION,但它将生成一个不能在其他用例中重复使用的查询.这得靠你自己了.

以下是我对您可以做的事情的简单看法

public function findNewestPosts(): array
{
        $qb = $this->createQueryBuilder('article');
        $qb->where('article.visible = 1')
            ->addSelect('image')
            ->andWhere('article.published_at <= :present_date')
            ->setParameter('present_date', date('Y-m-d H:i:s'))
            ->leftJoin('article.img', 'image')
            ->orderBy('article.id', 'DESC')
            ->setMaxResults(30);

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

public function findPostByIds(array $ids): array
{
        $qb = $this->createQueryBuilder('article');
        $qb->where('article.id IN ('.implode(',', $ids).')')
            ->andWhere('article.visible = 1')
            ->orderBy('article.id', 'DESC')
        return $qb->getQuery()->getResult();
}

请看:

->addSelect('image')

它会自动将图像添加到主select *,这是通过删除select()功能来完成的. 它将在调用$article->getImage();时阻止自动子查询(例如,如果您仅在twig中调用它,则相同) 对于这种情况,这是一个很好的性能优化,否则它将 for each 项目执行1个子查询.

然后在您的服务/控制器/其他内部:

$newestPosts = $yourRepository->findNewestPost();
$fixedPosts = $yourRepository->findPostByIds([1,2,3,4]);
$posts = array_merge($newestPosts, $fixedPosted);

Php相关问答推荐

我如何知道BaseController中当前运行的是哪个控制器?

具有重叠捕获组的PHP正则表达式

仅在WooCommerce管理订单视图上显示订单项自定义元数据

Laravel eloquent-如果没有包含InitialValue的结果,则查询where Second Value

有没有办法为整个PHP应用程序启用严格类型

将部分产品排除在WooCommerce的计算附加费之外

目标类[Set_Locale]不存在.拉威尔

我必须对所有的SQL查询使用预准备语句吗?

带Redhat php curl的http_code 0

PHP:为什么递归需要这么长时间

如何从phpseclib和SFTP获取日志(log)记录?

随机显示WordPress快捷代码功能时出现问题

服务器升级到新的mysql版本8.0.34后查询错误

Laravel 在数据库上显示加密列

Woocommerce 相关产品按其类别或排名数学主要类别

如何在 Laravel 迁移中使用日期时间和天数?

注册命名空间后如何获取xml node 值?

Composer自动加载器重复了子类的类文件路径

Laravel 路由参数中的 ":" 是什么?

使用循环和引用而不是递归创建分层/父子数组