Analog of features @WhereJoinTable and @JoinColumnsOrFormulas from Spring Hibernates. #6793

Open
opened 2026-01-22 15:38:48 +01:00 by admin · 0 comments
Owner

Originally created by @rosamarsky on GitHub (Aug 2, 2021).

Feature Request

Imagine I have a million entities, and each of them should be commentable.

so I create a table like this:

comments
id
object_type
object_id
...

Of course, you will say that I need to use Inheritance Table in mapping, but if I do this then I need to create million classes like EachClassComment extends Comment. So I want to have simple one-to-many relation with join by object_id = id AND object_type = <value>; I already tried do this dirty by this way and it works:

trait Commentable
{
    /**
     * @ORM\Column(type="string", name="entity_type")
     */
    private string $entityType;

    /**
     * @ORM\ManyToMany(targetEntity=Comment::class)
     * @ORM\JoinTable(name="comments",
     *      joinColumns={
     *          @ORM\JoinColumn(name="object_id", referencedColumnName="id"),
     *          @ORM\JoinColumn(name="object_type", referencedColumnName="entity_type")
     *      },
     *      inverseJoinColumns={@ORM\JoinColumn(name="id", referencedColumnName="id")}
     * )
     *
     * @var Collection|Comment[]
     */
    private Collection $comments;
}

As you see I wrote @ORM\JoinColumn(name="object_type", referencedColumnName="entity_type") that means I need to add column entity_type in DB for each commentable entities table.

I know in Hibernate(Java - Spring framework) this possible to do with different simple ways. For example:

@WhereJoinTable(clause="role='MODERATOR'")
@ManyToMany
@JoinTable(
    name = "r_user_group",
    joinColumns = @JoinColumn(name = "user_id"),
    inverseJoinColumns = @JoinColumn(name = "group_id")
)
private List<Group> moderatorGroups = new ArrayList<>();
@JoinColumnsOrFormulas({
    @JoinColumnOrFormula(formula=@JoinFormula(value="'A'", referencedColumnName="type")),
    @JoinColumnOrFormula(column=@JoinColumn("id", referencedColumnName="id"))
})
private TypeA inspectionSnapshot;

But Doctrine has no features like WhereJoinTable and JoinColumnOrFormula.

Q A
New Feature yes
RFC no
BC Break no

Summary

Nice to have at least one of the features written above. WhereJoinTable looks not difficult to make.
Even something like this @ORM\JoinColumn(name="object_type", value=Provider::ENTITY_NAME) partially solve this issue

Thanks guys.

Originally created by @rosamarsky on GitHub (Aug 2, 2021). ### Feature Request Imagine I have a million entities, and each of them should be commentable. so I create a table like this: | comments | | -- | | id | | object_type | | object_id | | ... | Of course, you will say that I need to use **Inheritance Table** in mapping, but if I do this then I need to create million classes like `EachClassComment extends Comment`. So I want to have simple one-to-many relation with `join by object_id = id AND object_type = <value>`; I already tried do this dirty by this way and it works: ```php trait Commentable { /** * @ORM\Column(type="string", name="entity_type") */ private string $entityType; /** * @ORM\ManyToMany(targetEntity=Comment::class) * @ORM\JoinTable(name="comments", * joinColumns={ * @ORM\JoinColumn(name="object_id", referencedColumnName="id"), * @ORM\JoinColumn(name="object_type", referencedColumnName="entity_type") * }, * inverseJoinColumns={@ORM\JoinColumn(name="id", referencedColumnName="id")} * ) * * @var Collection|Comment[] */ private Collection $comments; } ``` As you see I wrote `@ORM\JoinColumn(name="object_type", referencedColumnName="entity_type")` that means I need to add column `entity_type` in DB for each commentable entities table. I know in Hibernate(Java - Spring framework) this possible to do with different simple ways. For example: ```java @WhereJoinTable(clause="role='MODERATOR'") @ManyToMany @JoinTable( name = "r_user_group", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "group_id") ) private List<Group> moderatorGroups = new ArrayList<>(); ``` ```java @JoinColumnsOrFormulas({ @JoinColumnOrFormula(formula=@JoinFormula(value="'A'", referencedColumnName="type")), @JoinColumnOrFormula(column=@JoinColumn("id", referencedColumnName="id")) }) private TypeA inspectionSnapshot; ``` But Doctrine has no features like `WhereJoinTable` and `JoinColumnOrFormula`. | Q | A |------------ | ------ | New Feature | yes | RFC | no | BC Break | no #### Summary Nice to have at least one of the features written above. `WhereJoinTable` looks not difficult to make. Even something like this @ORM\JoinColumn(name="object_type", **value=Provider::ENTITY_NAME**) partially solve this issue Thanks guys.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6793