3
votes

I used ZF2, Doctrine

I have several Entity which extends base abstract class. One of fields in abstract class have relation with other Entity - message My Entity:

class AbstractChat
{
    ............

    /** ONE-TO-MANY BIDIRECTIONAL, INVERSE SIDE
     * @ORM\OneToMany(targetEntity="Chat\Entity\Message", mappedBy="chat")
     */
    protected $messages;
}

class Chat extends AbstractChat
{

}

class ChatBuilding extends AbstractChat
{

}

class Message
{

    /**
     * @var \Chat\Entity\Chat
     *
     * @ORM\ManyToOne(targetEntity="Chat\Entity\AbstractChat", inversedBy="messages")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="chat_id", referencedColumnName="id")
     * })
     */
    private $chat;
}

How can I state that the relation is with all entities which extend AbstractChat class?

This mean does not work:

 @ORM\ManyToOne(targetEntity="Chat\Entity\AbstractChat", inversedBy="messages")
1

1 Answers

3
votes

you can implement your relation with superclass .

https://www.doctrine-project.org/projects/doctrine-orm/en/2.8/reference/inheritance-mapping.html

inheritance has two type . single table and Class Table (table per class in hibernate).look at Class Table Inheritance