DQL with JOINs in Inheritance Type SINGLE_TABLE #6226

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

Originally created by @danielspk on GitHub (Apr 15, 2019).

Originally assigned to: @jwage on GitHub.

Support Question

Hello, how can I use relationships in DQL in classes with inheritance. Example:

/**
 * @Entity
 * @InheritanceType("SINGLE_TABLE")
 * @DiscriminatorColumn(name="type", type="integer")
 * @DiscriminatorMap({
 *   "1" = "App\Entity\ItemTypeA",
 *   "2" = "App\Entity\ItemTypeB"
 * })
 * @Table(name="items")
 */
abstract class ItemBase
{}

/**
 * @Entity 
 */
class ItemTypeA extends ItemBase
{
    /**
     * @OneToMany(targetEntity="Tag", mappedBy="itemTypeA")
     */
    private $tags;
}

/**
 * @Entity
 */
class ItemTypeB extends ItemBase
{}

/**
 * @Entity
 * @Table(name="tags)
 */
class Tag
{
    /**
     * @ManyToOne(targetEntity="ItemTypeA", inversedBy="tags")
     * @JoinColumn(name="itemTypeA_id", referencedColumnName="id")
     */
    private $itemTypeA;
}

/**
 * @Entity
 * @Table(name="invoices_items")
 */
class InvoiceItem
{
    /**
     * @ManyToOne(targetEntity="App\Entity\ItemBase")
     * @JoinColumn(name="itemBase_id", referencedColumnName="id")
     */
    private $item;
}

DQL use:

SELECT ii, i, t FROM App\Entity\InvoiceItem ii JOIN ii.item i JOIN i.tags t WHERE i INSTANCE OF App\Entity\ItemTypeA

Error produced: Class App\Entity\Item has no association named tags

Originally created by @danielspk on GitHub (Apr 15, 2019). Originally assigned to: @jwage on GitHub. ### Support Question Hello, how can I use relationships in DQL in classes with inheritance. Example: ``` /** * @Entity * @InheritanceType("SINGLE_TABLE") * @DiscriminatorColumn(name="type", type="integer") * @DiscriminatorMap({ * "1" = "App\Entity\ItemTypeA", * "2" = "App\Entity\ItemTypeB" * }) * @Table(name="items") */ abstract class ItemBase {} /** * @Entity */ class ItemTypeA extends ItemBase { /** * @OneToMany(targetEntity="Tag", mappedBy="itemTypeA") */ private $tags; } /** * @Entity */ class ItemTypeB extends ItemBase {} /** * @Entity * @Table(name="tags) */ class Tag { /** * @ManyToOne(targetEntity="ItemTypeA", inversedBy="tags") * @JoinColumn(name="itemTypeA_id", referencedColumnName="id") */ private $itemTypeA; } /** * @Entity * @Table(name="invoices_items") */ class InvoiceItem { /** * @ManyToOne(targetEntity="App\Entity\ItemBase") * @JoinColumn(name="itemBase_id", referencedColumnName="id") */ private $item; } ``` DQL use: ` SELECT ii, i, t FROM App\Entity\InvoiceItem ii JOIN ii.item i JOIN i.tags t WHERE i INSTANCE OF App\Entity\ItemTypeA ` Error produced: Class App\Entity\Item has no association named tags
admin added the Question label 2026-01-22 15:29:13 +01:00
admin closed this issue 2026-01-22 15:29:13 +01:00
Author
Owner

@jwage commented on GitHub (Apr 17, 2019):

Hi, you can try the #orm channel in Slack to ask questions or StackOverflow. Thanks!

@jwage commented on GitHub (Apr 17, 2019): Hi, you can try the #orm channel in [Slack](https://www.doctrine-project.org/slack) to ask questions or [StackOverflow](https://stackoverflow.com/questions/tagged/doctrine-orm). Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6226