Doctrine 2 query builder and leftJoin subquery

2020-07-12 07:23发布

I have the following entities.

Entity Product:

namespace App\Model;

use Nette\Object;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity
 */
class Product extends Object
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @var int
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Category")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id", onDelete="SET NULL")
     * @var int
     */
    private $category;

    /**
     * @ORM\OneToOne(targetEntity="Image", cascade={"persist", "remove"})
     * @var Image
     */
    private $image;

    /**
     * @ORM\OneToOne(targetEntity="Image", cascade={"persist", "remove"})
     * @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
     * @var Image
     */
    private $backgroundImage;

    /**
     * @ORM\ManyToOne(targetEntity="Layer")
     * @ORM\JoinColumn(name="methodOfPreparing_id", referencedColumnName="id", onDelete="SET NULL")
     * @var Layer|NULL
     */
    private $methodOfPreparing;
}

Entity Layer:

/**
 * @ORM\Entity
 */
class Layer extends Object
{    
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @var int
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="State", mappedBy="layer", indexBy="position", cascade={"persist", "remove"})
     * @var ArrayCollection
     */
    private $states;

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

    /**
     * @ORM\Column(type="integer")
     * @var int
     */
    private $position;
}

Entity State:

/**
 * @ORM\Entity
 */
class State extends Object
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @var int
     */
    private $id;

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

    /**
     * @ORM\ManyToOne(targetEntity="Layer", inversedBy="states")
     * @ORM\JoinColumn(name="layer_id", referencedColumnName="id", onDelete="CASCADE")
     * @var Layer
     */
    private $layer;

    /**
     * @ORM\Column(type="integer")
     * @var int
     */
    private $position;

    /**
     * @ORM\Column(type="integer", columnDefinition="int(1) NOT NULL")
     * @var bool
     */
    private $active;
}

skeleton query:

SELECT Product
    FROM product
        LEFT JOIN 
            (SELECT Layer, State
                FROM layer
                INNER JOIN state ON layer.id = state.layer_id
                WHERE state.active = 1
            ) sub
        ON sub.id = product.methodOfPreparing_id

I need to create a DQL query using the query builder. It is possible left join subquery?

1条回答
▲ chillily
2楼-- · 2020-07-12 07:45

You can do LEFT JOIN subqueries in DQL using a WITH clause as the JOIN criteria.

E.g.,

SELECT st
FROM SomeThings st
LEFT JOIN st.other ot WITH ot.id = (
  SELECT ot2
  FROM OtherThings ot2
  WHERE ot2.id = 'some ID'
)
WHERE blah blah blah

... or ...

SELECT st
FROM SomeThings st
LEFT JOIN st.others ot WITH ot.id IN (
  SELECT ot2
  FROM OtherThings ot2
  WHERE ot2.status = true
)
WHERE blah blah blah

... and the like.

Here are working samples from a codebase I work on:

SELECT f, df, d
FROM Entities\Main_frames f
LEFT JOIN f.dashboards df WITH df.dashboard IN (
  SELECT dd
  FROM Entities\Main_dashboards dd
  WHERE dd.dashboard_addedby = :createdBy
    AND dd.status = true
)
LEFT JOIN df.dashboard d
INDEX BY f.id
WHERE f.createdBy = :createdBy
  AND f.frame_type IN (2, 6)
  AND f.status = true
ORDER BY f.createdDate

... and ...

SELECT m, cm, s
FROM Entities\Main_modules m
LEFT JOIN m.companyModules cm WITH cm.companyid = :companyId
LEFT JOIN m.subscriptions s WITH s.id = (
  SELECT ss
  FROM Entities\Subscriptions ss
  WHERE ss.company = :companyId
    AND ss.module = m.id
    AND ss.createdDate = (
      SELECT MAX(sss.createdDate)
      FROM Entities\Subscriptions sss
      WHERE sss.company = :companyId
        AND sss.module = m.id
  )
)
WHERE m.id <= 4
ORDER BY m.id
查看更多
登录 后发表回答