Jean francois LEFLOCH
Jean francois LEFLOCH

Reputation: 83

One entity to many entities symfony

With doctrine on symfony, looking to an entity linked to multiple entities on a single remaining column. For example :

Entity engine extend two entities

one entity to many entities

I want to use a single column in engine, because I'm having a very large number of linked table.

I can not find what is the best practice in this kind of scheme. This is possible? how ?

thanks in advance

Upvotes: 2

Views: 148

Answers (1)

Stephan Vierkant
Stephan Vierkant

Reputation: 10144

You can do this with 'Class Table Inheritance'. Your code will look like this:

namespace MyProject\Model;

/**
 * @Entity
 * @InheritanceType("JOINED")
 * @DiscriminatorColumn(name="discr", type="string")
 * @DiscriminatorMap({"car" = "Car", "plane" = "Plane"})
 */
class Vehicle
{
    // ...
}

/** @Entity */
class Plane extends Vehicle
{
    // ...
}


/** @Entity */
class Car extends Vehicle
{
    // ...
}

This solves your problem of having only one foreign key on your Engine table. It also helps you have a more clear code when you have other 'shared' properties (for example a date of manufacture)

Upvotes: 2

Related Questions