Join second entity if record available #6254

Closed
opened 2026-01-22 15:29:38 +01:00 by admin · 1 comment
Owner

Originally created by @johnmitz on GitHub (Jun 21, 2019).

Originally assigned to: @Ocramius on GitHub.

The issue I am facing now is I have two entities, where I can't modify first entity.
Example of first entity

/**
 * @ORM\Entity
 * @ORM\Table(name="product")
 */
class Product
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
 
    ....
}

Example of second entity:

/**
 * @ORM\Entity
 * @ORM\Table(name="order")
 */
class Order
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="\Product")
     * @ORM\JoinTable(name="order_product",
     *      joinColumns={
     *          @ORM\JoinColumn(name="order_id", referencedColumnName="id")
     *       },
     *      inverseJoinColumns={
     *          @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     *      }
     * )
     */
    protected $articles;
}

Now what I need to do is to fetch all products and include orders if they are available for the product, if not I can just add field with false value or something, but products even which has no orders, still has to be fetched. Is this possible?

Originally created by @johnmitz on GitHub (Jun 21, 2019). Originally assigned to: @Ocramius on GitHub. The issue I am facing now is I have two entities, where I can't modify first entity. Example of first entity ``` /** * @ORM\Entity * @ORM\Table(name="product") */ class Product { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; .... } ``` Example of second entity: ``` /** * @ORM\Entity * @ORM\Table(name="order") */ class Order { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var ArrayCollection * * @ORM\ManyToMany(targetEntity="\Product") * @ORM\JoinTable(name="order_product", * joinColumns={ * @ORM\JoinColumn(name="order_id", referencedColumnName="id") * }, * inverseJoinColumns={ * @ORM\JoinColumn(name="product_id", referencedColumnName="id") * } * ) */ protected $articles; } ``` Now what I need to do is to fetch all products and include orders if they are available for the product, if not I can just add field with false value or something, but products even which has no orders, still has to be fetched. Is this possible?
admin added the Question label 2026-01-22 15:29:38 +01:00
admin closed this issue 2026-01-22 15:29:38 +01:00
Author
Owner

@Ocramius commented on GitHub (Jun 22, 2019):

SELECT p, o FROM Product p LEFT JOIN p.orders o

This gives you Product instances with populated Order entries. If you don't have the association defined from the Product side, then you will need two separate DQL queries (one to fetch products, one to fetch orders by product id).

@Ocramius commented on GitHub (Jun 22, 2019): `SELECT p, o FROM Product p LEFT JOIN p.orders o` This gives you `Product` instances with populated `Order` entries. If you don't have the association defined from the `Product` side, then you will need two separate DQL queries (one to fetch products, one to fetch orders by product id).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6254