【问题标题】:Struggling making a Querybuilder with SQL request for Symfony努力为 Symfony 制作带有 SQL 请求的 Querybuilder
【发布时间】:2021-02-12 21:17:32
【问题描述】:

我是一个初学者,我正在 Symfony 中做一个应用程序,但是我在提出请求时遇到了问题,有人可以帮助我吗? 我有 2 个表,我需要获取有命令的用户列表,我发出了 SQL 请求:

SELECT c.user_id, u.* FROM commande AS c LEFT JOIN user AS u ON c.user_id = u.id;

但我很难为其创建查询构建器,我想知道是否应该在 CommandeRepo 或 UserRepo 中进行查询

here are the tables

感谢您的帮助!

【问题讨论】:

标签: sql symfony doctrine


【解决方案1】:

您想要检索users who have an order,因此您的查询应该在 UserRepository 中。

public function getUsersThatHaveAtLeastOneCommand()
{
    $qb = $this->createQueryBuilder('u')
    // commandes here is the name of the property in User entity that contains your "commandes"
    $qb->innerJoin('u.commandes', 'c')

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

我认为这应该足以让您返回至少执行过一个命令的用户。但是请在本地进行一些测试以确保:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多