Load other entities by class name and id fields in Entity on Doctrine #5094

Closed
opened 2026-01-22 14:58:06 +01:00 by admin · 1 comment
Owner

Originally created by @aistis- on GitHub (Apr 12, 2016).

Originally assigned to: @Ocramius on GitHub.

I am looking an approach for following case. I do have an entity which keeps information about other entity which could be any type of entuty. The subentity is recognized by field combination subject_class and subject_id. Is there a way that Doctrine would automatically fetch this subentity whenever I am fetching the parent?

Parent entity example:

<?php

namespace Yolo\RandomBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(
 *      uniqueConstraints={
 *          @ORM\UniqueConstraint(columns={"subject_id", "subject_class"})
 *      }
 * )
 */
class SwagRelation
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

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

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

    /**
     * @var object
     */
    private $subject;

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return object
     */
    public function getSubject()
    {
        // Here should be returned an entity got from subject class and id!
        return $this->subject;
    }

    /**
     * @param object $subject
     *
     * @return SwagRelation
     */
    public function setSubject($subject)
    {
        $this->subject = $subject;
        $this->subjectClass = get_class($subject);
        $this->subjectId = $subject->getId();

        return $this;
    }
}
Originally created by @aistis- on GitHub (Apr 12, 2016). Originally assigned to: @Ocramius on GitHub. I am looking an approach for following case. I do have an entity which keeps information about other entity which could be any type of entuty. The subentity is recognized by field combination `subject_class` and `subject_id`. Is there a way that Doctrine would automatically fetch this subentity whenever I am fetching the parent? Parent entity example: ``` <?php namespace Yolo\RandomBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table( * uniqueConstraints={ * @ORM\UniqueConstraint(columns={"subject_id", "subject_class"}) * } * ) */ class SwagRelation { /** * @var int * * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(type="string") */ private $subjectClass; /** * @var string * * @ORM\Column(type="integer") */ private $subjectId; /** * @var object */ private $subject; /** * @return int */ public function getId() { return $this->id; } /** * @return object */ public function getSubject() { // Here should be returned an entity got from subject class and id! return $this->subject; } /** * @param object $subject * * @return SwagRelation */ public function setSubject($subject) { $this->subject = $subject; $this->subjectClass = get_class($subject); $this->subjectId = $subject->getId(); return $this; } } ```
admin added the Invalid label 2026-01-22 14:58:06 +01:00
admin closed this issue 2026-01-22 14:58:08 +01:00
Author
Owner

@Ocramius commented on GitHub (Apr 12, 2016):

This kind of problem/solution is documented at http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/cookbook/strategy-cookbook-introduction.html

@Ocramius commented on GitHub (Apr 12, 2016): This kind of problem/solution is documented at http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/cookbook/strategy-cookbook-introduction.html
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5094