简体   繁体   中英

ManyToOne association mapping to a Class Table Inheritance entity in Doctrine 2

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM