gpt4 book ai didi

php - createQueryBuilder/加入列 - Symfony

转载 作者:行者123 更新时间:2023-11-30 22:13:11 24 4
gpt4 key购买 nike

我在执行这个 SQL 请求时遇到了麻烦:

SELECT *
FROM project
LEFT JOIN user
ON project.idAuthor=user.id
WHERE project.isVisible = 1 AND
user.role = 'agency'

到一个简单的 Symfony 查询生成器:

 $query = $this->createQueryBuilder('p')
->leftJoin('WebAwardsBundle:User', 'u')
->where('p.isVisible = 1')
->andwhere("u.role = 'agency'")
->orderBy('p.id', 'DESC')
->getQuery();

此查询的响应为我提供了所有项目,包括角色时的项目!== agency ...

我不知道我可以把 ON project.idAuthor=user.id 放在哪里

映射:

项目:

    /**
* Project
*
* @ORM\Table(name="project")
*@ORM\Entity(repositoryClass="WebAwardsBundle\Repostory\ProjectRepository")
*/
class Project
{
...
/**
* @var int
*
* @ORM\ManyToOne(targetEntity="User", inversedBy="projects")
* @ORM\JoinColumn(name="idAuthor", referencedColumnName="id")
*/
private $idAuthor;

...

用户:

/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="WebAwardsBundle\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable{

...

/**
* @ORM\OneToMany(targetEntity="Project", mappedBy="idAuthor")
*/
private $projects;
...

最佳答案

执行此 SQL 请求的正确答案:

SELECT *
FROM project
LEFT JOIN user
ON project.idAuthor=user.id
WHERE project.isVisible = 1 AND
user.role = 'agency'

是加入实体的正确列(在本例中为用户的 project.idAuthor):

$query = $this->createQueryBuilder('p')
->join('p.idAuthor', 'u')
->where('p.isVisible = 1')
->andWhere("u.role = 'agency'")
->orderBy('p.id', 'DESC')
->getQuery();

关于php - createQueryBuilder/加入列 - Symfony,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39351808/

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