ManyToOne association mapping to a Class Table Inh

2020-07-27 05:35发布

问题:

I have an Author entity, which is a Class Table Inheritance containing an AuthorUser and an AuthorGroup.

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

AuthorUser relates to my User entity and AuthorGroup to my Group entity.

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 inverse this. Anyway, the problem is that i have to add this CTI to my Article entity field. How can i relate using ManyToOne to this Article entity field?

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 either an User or Group object to the author field. I followed this behavior, but it doesn't seem to help. It gets even more complicated.

回答1:

One solution could be to always have AuthorGroups, even when there's only one Author.

Otherwise, take a look at https://github.com/FabienPennequin/DoctrineExtensions-Rateable

You might be able to use that code to provide a similar Authored interface that can discriminate between the AuthorUser and AuthorGroup.