api-platform:嵌套对象集合过滤无效
创始人
2024-09-07 05:00:42
0

可以使用Doctrine的QueryBuilder,通过嵌套关联来解决这个问题。

下面是一个示例,假设有两个实体,Article和Comment,并且每个Article都有多个Comment嵌套在其中:

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;

class Article { private $id; private $title; private $comments;

public function __construct()
{
    $this->comments = new ArrayCollection();
}

// Getters and setters...

public function addComment(Comment $comment)
{
    $this->comments->add($comment);
    $comment->setArticle($this);
}

public function removeComment(Comment $comment)
{
    $this->comments->removeElement($comment);
    $comment->setArticle(null);
}

}

namespace App\Entity;

class Comment { private $id; private $content; private $article;

// Getters and setters...

public function setArticle(Article $article = null)
{
    $this->article = $article;
}

public function getArticle(): ?Article
{
    return $this->article;
}

}

现在,假设我们要获取所有标题中包含“api”的文章,这些文章中的评论必须包含内容“great”。我们可以使用以下代码:

namespace App\Repository;

use App\Entity\Article; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Common\Collections\Criteria; use Doctrine\ORM\Query\Expr; use Doctrine\Persistence\ManagerRegistry;

class ArticleRepository extends ServiceEntityRepository { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, Article::class); }

public function findArticlesWithGreatComments(string $title)
{
    $qb = $this->createQueryBuilder('a');
    $qb
        ->join('a.comments', 'c')
        ->where(
            $qb->expr()->andX(
                $qb->expr()->like('a.title', ':title'),
                $qb->expr()->eq('c.content', ':content')
            )
        )
        ->setParameters

相关内容

热门资讯

查到实测!福建兄弟13水辅助,... 查到实测!福建兄弟13水辅助,川游游戏辅助,详细技巧(有挂技巧)1、游戏颠覆性的策略玩法,独创攻略技...
实测分享!陕麻全黑科技,纳祥游... 实测分享!陕麻全黑科技,纳祥游戏链接辅助,详细脚本(有挂教程)1、纳祥游戏链接辅助辅助软件下载优化,...
盘点一款!微信边锋辅助挂件,同... 盘点一款!微信边锋辅助挂件,同城上饶辅助,详细工具(有挂教学)1、微信边锋辅助挂件辅助器安装包、微信...
让我来分享经验!微友辅助神器下... 让我来分享经验!微友辅助神器下载,牵手跑辅助,详细插件(有挂攻略)1、打开软件启动之后找到中间准星的...
科技通报!雀友会广东潮汕苹果开... 科技通报!雀友会广东潮汕苹果开挂,决战十三水ios辅助,详细器(确实有挂)1、玩家可以在雀友会广东潮...
我来教大家!蜀山四川破解好友版... 我来教大家!蜀山四川破解好友版辅助,乐酷副厅辅助,详细挂(真实有挂)1、蜀山四川破解好友版辅助免费辅...
技巧知识分享!蘑菇云辅助怎么使... 技巧知识分享!蘑菇云辅助怎么使用,微信神兽大厅源代码,详细技巧(有挂教学)微信神兽大厅源代码破解侠是...
重大通报!陕西三代攻略,雀神广... 重大通报!陕西三代攻略,雀神广东麻将必赢方法,详细插件(讲解有挂)1、下载好雀神广东麻将必赢方法正确...
教程辅助!随意玩俱乐部辅助,皇... 教程辅助!随意玩俱乐部辅助,皇豪互娱控制系统,详细器(有挂技术)1、随意玩俱乐部辅助免费脚本咨询教程...
必看攻略!闽游app辅助器,枫... 必看攻略!闽游app辅助器,枫叶辅助官网下载,详细插件(今日头条)1、枫叶辅助官网下载破解器简单,枫...