内部加入Symfony查询构建器

时间:2018-10-17 19:36:36

标签: php symfony doctrine

我在QueryBuilder中编写此SQL查询时遇到问题:

SELECT SUM(TableA.SUM) AS TOTAL, TableB.name 
    FROM TableA INNER JOIN TableB ON TableA.category_id = TableB.id GROUP BY TableB.name

当我将其调用到mysql查询中时它就起作用了。我得到类似的东西:

类别|总 A类| 40 b类| 67 c类| 5

我的支出实体类:

/**
 * Expenditure
 *
 * @ORM\Table(name="accountant_expenditure")
 * @ORM\Entity(repositoryClass="accountant\ExpenditureBundle\Repository\ExpenditureRepository")
 */
class Expenditure
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="accountant\UserBundle\Entity\User")
     * @ORM\JoinColumn(name="create_by_user_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $createBy;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="create_at", type="datetime")
     */
    private $createAt;

    /**
     * @var integer
     *
     * @ORM\ManyToOne(targetEntity="accountant\ExpenditureBundle\Entity\ExpenditureCategories")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $category;

    /**
     * @var float
     *
     * @ORM\Column(name="sum", type="float")
     */
    private $sum;
//...

ExpenditureCategories实体类:

/**
 * ExpenditureCategories
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="accountant\ExpenditureBundle\Repository\ExpenditureCategoriesRepository")
 */
class ExpenditureCategories
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;
//...

我写这样的东西,但这是不正确的:

public function findCurrentMonthExpendituresSumByCategory()
    {
        $firstDay = new \DateTime('first day of this month');
        $lastDay = new \DateTime('last day of this month');

        return $this->createQueryBuilder('e')
            ->select('SUM(e.sum) AS TOTAL, c.name')
            ->from('ExpenditureBundle:ExpenditureCategories', 'c')
            ->leftJoin('e.category', 'c')
            ->where('e.createAt BETWEEN :start AND :end')
            ->setParameter('start', $firstDay->format('Y-m-d'))
            ->setParameter('end', $lastDay->format('Y-m-d'))
            ->groupBy('c.name')
            ->getQuery()
            ->getResult();
    }

也许我的实体注释存在问题? 我得到:

[Semantical Error] line 0, col 154 near 'c WHERE e.createAt': Error: 'c' is already defined.

还是非常感谢您的帮助;)

2 个答案:

答案 0 :(得分:0)

就在这里

->from('ExpenditureBundle:ExpenditureCategories', 'c')
        ->leftJoin('e.category', 'c')

c处以c左加入,您使用相同的别名2x。

数据库抱怨它已经定义:

  

[语义错误]第0行,'c WHERE e.createAt'附近的col 154:错误:'c'已经定义

这应该是(我将leftJoin更改为join):

  ->from('ExpenditureBundle:ExpenditureCategories', 'e')
     ->join('e.category', 'c')

虽然我很确定别名是第二个参数,但我对《 Doctrine》的演奏感觉很短。

基本上,您想将e.category_id加入c.id

更新

谈到加入(内部加入)与左加入。区别在于,如果两个表(联接的两面)都存在一行,则联接将仅返回结果。对于左联接,只需要在第一个表中有数据,而右联接则相反。

示例是“帖子和评论”。您可能要使用查询将帖子和评论一起拉出。如果使用“加入”,则只会收到有评论的帖子,如果使用左连接,则只会得到没有评论的帖子。如果没有,注释表的那些字段将为NULL。

答案 1 :(得分:0)

我随您的写作而变化,我必须评论->从,以获得正确的结果。

 public function findCurrentMonthExpendituresSumByCategory()
    {
        $firstDay = new \DateTime('first day of this month');
        $lastDay = new \DateTime('last day of this month');

        return $this->createQueryBuilder('e')
            ->select('SUM(e.sum) AS TOTAL, c.name')
            //->from('ExpenditureBundle:ExpenditureCategories', 'e')
            ->leftJoin('e.category', 'c')
            ->where('e.createAt BETWEEN :start AND :end')
            ->setParameter('start', $firstDay->format('Y-m-d'))
            ->setParameter('end', $lastDay->format('Y-m-d'))
            ->groupBy('c.name')
            ->getQuery()
            ->getResult();
    }

现在正在工作。 非常感谢你。