PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 15, 2022

[FIXED] One entity to many entities symfony

 February 15, 2022     doctrine, doctrine-orm, php, symfony     No comments   

Issue

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

Entity engine extend two entities

  • Entity plane
  • Entity car

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


Solution

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)



Answered By - Stephan Vierkant
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing