S
S
symnoob2020-04-18 02:00:45
symfony
symnoob, 2020-04-18 02:00:45

Symfony 4 - How to solve problem with Entity in Bundle?

Hello everyone,

I have a task, I wanted to create a Symfony Bundle.
For example, Bundle animals, there is an Animals entity.

This bundle should work in different projects.
In one project, the Animals entity by itself, in other projects, the Animals entity must have different ManyToOne, OneToMany, and ManyToMany associations and relationships to all other entities.

How can this be implemented?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BoShurik, 2020-04-19
@symnoob

You need a Mapped Superclass

// Этот класс в бандле
/** @MappedSuperclass */
class BaseAnimal
{
    /** @Column(type="string") */
    protected $name;

    /** @Column(type="integer") */
    protected $size;
    
    /** @Column(type="boolean")  */
    protected $canFly;
}

// Этот класс в проекте
/** @Entity */
class Animal extends BaseAnimal
{
    /** @Column(type="string") */
    private $id;

    /** @ManyToOne(targetEntity="Person")  */
    private $owner;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question