Simplified entities

/** @ORM\InheritanceType("SINGLE_TABLE") */
abstract class Post extends Identified
{
    /** @ORM\Column(type="text", nullable=FALSE) */
    private $content;

    /** @ORM\ManyToOne(targetEntity="\Archivist\Users\User") */
    private $user;
}

class Question extends Post
{
    /** @ORM\Column(type="string", nullable=FALSE) */
    private $title;

    /** @ORM\ManyToOne(targetEntity="Answer") */
    protected $solution;

    /** @ORM\OneToMany(targetEntity="Answer", mappedBy="question") */
    protected $answers;
}

class Answer extends Post
{
    /** @ORM\ManyToOne(targetEntity="Question", inversedBy="answers") */
    protected $question;
}

Now when I wanna query all posts, meaning I'll get mix of answers and questions, there is no way to optimize this call

$post->isQuestion() ? $post->title : $post->question->title;

Because you cannot join in DQL the relation of Answer entity, when you're selecting from Post. On Doctrine issue tracker, this was marked as “feature”, becase such DQL would violate OO design.

So there goes my question: how to design the relation better?

  • Do I have just all the posts (answers and questions) in one entity?
  • Do I have two separate entities (and therefore two tables)? Querying all the posts would also be complicated here.
  • Something better?

You must first log in to participate in this discussion