gpt4 book ai didi

php - 主义Mysql : multiple joins in query builder

转载 作者:太空宇宙 更新时间:2023-11-03 11:45:19 30 4
gpt4 key购买 nike

我有实体:TagUserServiceTagUserService 都是多对多关系。

 class Service{
/**
* @ORM\ManyToMany(targetEntity="Tag", mappedBy="serviceList")
*/
private $tagList;
}

class User{
/**
* @ORM\ManyToMany(targetEntity="Tag", mappedBy="userList")
*/
private $tagList;
}

class Tag{
/**
* @ORM\ManyToMany(targetEntity="User", inversedBy="tagList")
* @ORM\JoinTable(name="tags_users")
*/
private $userList;

/**
* @ORM\ManyToMany(targetEntity="Service", inversedBy="tagList")
* @ORM\JoinTable(name="tags_services")
*/
private $serviceList;
}

使用查询构建器,我怎样才能返回所有至少有 1 个公共(public)标签的给定 User 服务,即与用户相关的服务

这是我写的一个查询,但它不起作用

  $query = $em->getRepository('AppBundle:Service')
->createQueryBuilder('service')
->join('service.tagList', 'serviceTag')
->join('AppBundle:BasicUser', 'user')
->join('user.tagList', 'userTag')
->where('user.id = :id')
->andWhere('serviceTag.id = userTag.id')
->setParameter('id', $user->getId())
;

错误:

 [Syntax Error] line 0, col 104: Error: Expected Literal, got "JOIN" (500 Internal Server Error)

最佳答案

您的查询是错误的。我认为最简单的解决方案是:

 $query = $em->getRepository('AppBundle:Service')
->createQueryBuilder('service')
->innerJoin('service.tagList', 'tag')
->andWhere(':user MEMBER OF tag.userList')
->setParameter('user',$user)
;

关于php - 主义Mysql : multiple joins in query builder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39246251/

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