Associating ManyToOne Associations with the Class Inheritance Object in Doctrine 2

I have an Author object, which is a class table inheritance containing AuthorUser and AuthorGroup .

 /** * Author * * @ORM\Table * @ORM\Entity * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="type", type="string") * @ORM\DiscriminatorMap({"user" = "AuthorUser", "group" = "AuthorGroup"}) */ class Author { // ... } 

AuthorUser refers to my User object and AuthorGroup to my Group object.

 class AuthorUser extends Author { /** * @var User * * @ORM\ManyToOne(targetEntity="User", inversedBy="?????") * @ORM\JoinColumn(name="user_id", referencedColumnName="id") */ protected $user; } class AuthorGroup extends Author { /** * @var Group * * @ORM\ManyToOne(targetEntity="Group", inversedBy="?????") * @ORM\JoinColumn(name="group_id", referencedColumnName="id") */ protected $user; } 

I have no idea how to do this. Anyway, the problem is that I have to add this CTI to my Article object field. How can I associate the use of ManyToOne with this field of an Article object?

 class Article { /** * @var Author * * @ORM\ManyToOne(targetEntity="Author", inversedBy="?????????") * @ORM\JoinColumn(name="author_id", referencedColumnName="id") */ protected $author; } 

I'm not sure how to make this as transparent as possible. When I create a new Article , I need to provide a User or Group object in the Author field. I followed this behavior, but it doesn't seem to help. It gets even harder.

+1
source share
1 answer

One solution may be to always have AuthorGroups, even if there is only one Author.

Otherwise, see https://github.com/FabienPennequin/DoctrineExtensions-Rateable

You may be able to use this code to create a similar authoring interface that can distinguish between AuthorUser and AuthorGroup.

0
source

Source: https://habr.com/ru/post/1440825/


All Articles