Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 map entity to different tables

I have my entity foo which contains the properties id, bar and baz. Can I populate two different tables (like foo1 and foo2) with the same entity, based on the property baz which is not mapped. The code of the entity looks like this :

class foo {

  /**
  * @ORM\Column(name="id", type="integer")
  * @ORM\Id
  */      
  private $id;

  /**
  * @ORM\Column(name="bar", type="string")
  */
  private $bar;

  /**
  * Property not mapped in the database
  */
  private $baz; 
}

I want if the value of baz is 1, to save the entity in the table foo1, and if the value of baz is 2, to save in the table foo2. Where can I select in which table it would save ?

like image 660
Gabriel Diaconescu Avatar asked Nov 29 '25 12:11

Gabriel Diaconescu


1 Answers

Use doctrine inheritance:

Doctrine inheritance

It works like this:

BaseClient

  • SubClient1 extends from BaseClient
  • SubClient2 extends from BaseClient

Like that you can even add extra fields to let's say SubClient1 or SubClient2 who are specific only for that entity.

like image 138
Baba Yaga Avatar answered Dec 01 '25 00:12

Baba Yaga