Doctrine|ORM|Symfony: Is possible relation to Interface or multiple entities

Simple example:
I’ve got two users Admin and Client (both implements UserInterface) and Cart – three entity classes at a. Admin and Client can have his own carts. How to configure/resolve Cart entity relation to have method ‘getUser()’ which returns Admin or Client user?

Maybe I can have column user_id and second column with user entity name in Cart (something similar as DiscriminatorMapping can do)?

    class Admin implements UserInterface
    {

    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;
    
    /**
     * @var Collection
     * @ORMOneToMany(targetEntity="Cart", mappedBy="???")
     */
    private $carts;
    ....
    class Client implements UserInterface
    {

    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;
    
    /**
     * @var Collection
     * @ORMOneToMany(targetEntity="Cart", mappedBy="???")
     */
    private $carts;
    ....
    class Cart
    {

    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;
    
    /**
     * @var UserInterface
     * @ORMManyToOne(targetEntity="UserInterface", ???)
     */
    private $user;
    ....

I tried this Doctrine feature, also tried DisciminatorMapping and composite keys (join by multiple columns) option with no luck.

Any help?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *