Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can be Doctrine make relationship through abstract class

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")
like image 522
St_5 Avatar asked Dec 06 '25 11:12

St_5


1 Answers

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

like image 63
Amin Arab Avatar answered Dec 09 '25 15:12

Amin Arab



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!