You've already forked symfony-playground
inheritance
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
@ -12,11 +13,13 @@ class IndexController extends AbstractController
|
||||
#[Route('/', name: 'app_index')]
|
||||
public function index(Entity1Repository $repo): Response
|
||||
{
|
||||
$sm = $repo->getSchemaManager();
|
||||
$entity8 = $repo->findOneByName('entity 8');
|
||||
|
||||
return $this->render('index/index.html.twig', [
|
||||
'controller_name' => 'IndexController',
|
||||
'entity8' => $entity8
|
||||
'entity8' => $entity8,
|
||||
'schemaManager' => $sm
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
26
src/DataFixtures/InheritanceFixtures.php
Normal file
26
src/DataFixtures/InheritanceFixtures.php
Normal file
@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\DataFixtures;
|
||||
|
||||
use App\Entity\Employee1;
|
||||
use App\Entity\Toothbrush1;
|
||||
use Doctrine\Bundle\FixturesBundle\Fixture;
|
||||
use Doctrine\Persistence\ObjectManager;
|
||||
|
||||
class InheritanceFixtures extends Fixture
|
||||
{
|
||||
public function load(ObjectManager $manager): void
|
||||
{
|
||||
$e1 = new Employee1();
|
||||
$e1->setJob('peon')->setName('pepito')->setAge(20)->setToothbrush((new Toothbrush1())->setBrand('colgate'));
|
||||
$manager->persist($e1);
|
||||
|
||||
$e1 = new Employee1();
|
||||
$e1->setJob('jefe')->setName('benganito')->setAge(30)->setToothbrush((new Toothbrush1())->setBrand('signal'));
|
||||
$manager->persist($e1);
|
||||
|
||||
$manager->flush();
|
||||
}
|
||||
}
|
||||
|
||||
// symfony console doctrine:fixtures:load
|
55
src/Entity/Employee1.php
Normal file
55
src/Entity/Employee1.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity]
|
||||
class Employee1 extends Person1
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $id;
|
||||
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $job;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Employee1
|
||||
*/
|
||||
public function setId(int $id): Employee1
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getJob(): string
|
||||
{
|
||||
return $this->job;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $job
|
||||
* @return Employee1
|
||||
*/
|
||||
public function setJob(string $job): Employee1
|
||||
{
|
||||
$this->job = $job;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
55
src/Entity/Employee2.php
Normal file
55
src/Entity/Employee2.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity]
|
||||
class Employee2 extends Person2
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $id;
|
||||
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $job;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Employee2
|
||||
*/
|
||||
public function setId(int $id): Employee2
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getJob(): string
|
||||
{
|
||||
return $this->job;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $job
|
||||
* @return Employee2
|
||||
*/
|
||||
public function setJob(string $job): Employee2
|
||||
{
|
||||
$this->job = $job;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
55
src/Entity/Employee3.php
Normal file
55
src/Entity/Employee3.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity]
|
||||
class Employee3 extends Person3
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $id;
|
||||
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $job;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Employee3
|
||||
*/
|
||||
public function setId(int $id): Employee3
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getJob(): string
|
||||
{
|
||||
return $this->job;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $job
|
||||
* @return Employee3
|
||||
*/
|
||||
public function setJob(string $job): Employee3
|
||||
{
|
||||
$this->job = $job;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
75
src/Entity/Person1.php
Normal file
75
src/Entity/Person1.php
Normal file
@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\MappedSuperclass]
|
||||
class Person1
|
||||
{
|
||||
#[ORM\Column(type: 'integer')]
|
||||
protected int $age;
|
||||
|
||||
#[ORM\Column(type: 'string')]
|
||||
protected string $name;
|
||||
|
||||
#[ORM\OneToOne(targetEntity: 'Toothbrush1', cascade: ['persist'])]
|
||||
#[ORM\JoinColumn(name: 'toothbrush1_id', referencedColumnName: 'id')]
|
||||
protected Toothbrush1 $toothbrush;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getAge(): int
|
||||
{
|
||||
return $this->age;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $age
|
||||
* @return Person1
|
||||
*/
|
||||
public function setAge(int $age): Person1
|
||||
{
|
||||
$this->age = $age;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Person1
|
||||
*/
|
||||
public function setName(string $name): Person1
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Toothbrush1
|
||||
*/
|
||||
public function getToothbrush(): Toothbrush1
|
||||
{
|
||||
return $this->toothbrush;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Toothbrush1 $toothbrush
|
||||
* @return Person1
|
||||
*/
|
||||
public function setToothbrush(Toothbrush1 $toothbrush): Person1
|
||||
{
|
||||
$this->toothbrush = $toothbrush;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
101
src/Entity/Person2.php
Normal file
101
src/Entity/Person2.php
Normal file
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\InheritanceType('SINGLE_TABLE')]
|
||||
#[ORM\DiscriminatorColumn(name: 'discr', type: 'string')]
|
||||
#[ORM\DiscriminatorMap(['person2' => 'Person2', 'employee2' => 'Employee2'])]
|
||||
class Person2
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $id;
|
||||
|
||||
#[ORM\Column(type: 'integer')]
|
||||
protected int $age;
|
||||
|
||||
#[ORM\Column(type: 'string')]
|
||||
protected string $name;
|
||||
|
||||
#[ORM\OneToOne(targetEntity: 'Toothbrush2', cascade: ['persist'])]
|
||||
#[ORM\JoinColumn(name: 'toothbrush2_id', referencedColumnName: 'id')]
|
||||
protected Toothbrush2 $toothbrush;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Person2
|
||||
*/
|
||||
public function setId(int $id): Person2
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getAge(): int
|
||||
{
|
||||
return $this->age;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $age
|
||||
* @return Person2
|
||||
*/
|
||||
public function setAge(int $age): Person2
|
||||
{
|
||||
$this->age = $age;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Person2
|
||||
*/
|
||||
public function setName(string $name): Person2
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Toothbrush2
|
||||
*/
|
||||
public function getToothbrush(): Toothbrush2
|
||||
{
|
||||
return $this->toothbrush;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Toothbrush2 $toothbrush
|
||||
* @return Person2
|
||||
*/
|
||||
public function setToothbrush(Toothbrush2 $toothbrush): Person2
|
||||
{
|
||||
$this->toothbrush = $toothbrush;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
101
src/Entity/Person3.php
Normal file
101
src/Entity/Person3.php
Normal file
@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\InheritanceType('JOINED')]
|
||||
#[ORM\DiscriminatorColumn(name: 'discr', type: 'string')]
|
||||
#[ORM\DiscriminatorMap(['person3' => 'Person3', 'employee3' => 'Employee3'])]
|
||||
class Person3
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $id;
|
||||
|
||||
#[ORM\Column(type: 'integer')]
|
||||
protected int $age;
|
||||
|
||||
#[ORM\Column(type: 'string')]
|
||||
protected string $name;
|
||||
|
||||
#[ORM\OneToOne(targetEntity: 'Toothbrush3', cascade: ['persist'])]
|
||||
#[ORM\JoinColumn(name: 'toothbrush3_id', referencedColumnName: 'id')]
|
||||
protected Toothbrush3 $toothbrush;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Person3
|
||||
*/
|
||||
public function setId(int $id): Person3
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getAge(): int
|
||||
{
|
||||
return $this->age;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $age
|
||||
* @return Person3
|
||||
*/
|
||||
public function setAge(int $age): Person3
|
||||
{
|
||||
$this->age = $age;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Person3
|
||||
*/
|
||||
public function setName(string $name): Person3
|
||||
{
|
||||
$this->name = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Toothbrush3
|
||||
*/
|
||||
public function getToothbrush(): Toothbrush3
|
||||
{
|
||||
return $this->toothbrush;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Toothbrush3 $toothbrush
|
||||
* @return Person3
|
||||
*/
|
||||
public function setToothbrush(Toothbrush3 $toothbrush): Person3
|
||||
{
|
||||
$this->toothbrush = $toothbrush;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
}
|
54
src/Entity/Toothbrush1.php
Normal file
54
src/Entity/Toothbrush1.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity]
|
||||
class Toothbrush1
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $id;
|
||||
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $brand;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Toothbrush1
|
||||
*/
|
||||
public function setId(int $id): Toothbrush1
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBrand(): string
|
||||
{
|
||||
return $this->brand;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $brand
|
||||
* @return Toothbrush1
|
||||
*/
|
||||
public function setBrand(string $brand): Toothbrush1
|
||||
{
|
||||
$this->brand = $brand;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
54
src/Entity/Toothbrush2.php
Normal file
54
src/Entity/Toothbrush2.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity]
|
||||
class Toothbrush2
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $id;
|
||||
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $brand;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Toothbrush2
|
||||
*/
|
||||
public function setId(int $id): Toothbrush2
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBrand(): string
|
||||
{
|
||||
return $this->brand;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $brand
|
||||
* @return Toothbrush2
|
||||
*/
|
||||
public function setBrand(string $brand): Toothbrush2
|
||||
{
|
||||
$this->brand = $brand;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
54
src/Entity/Toothbrush3.php
Normal file
54
src/Entity/Toothbrush3.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity]
|
||||
class Toothbrush3
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $id;
|
||||
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $brand;
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getId(): int
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Toothbrush3
|
||||
*/
|
||||
public function setId(int $id): Toothbrush3
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getBrand(): string
|
||||
{
|
||||
return $this->brand;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $brand
|
||||
* @return Toothbrush3
|
||||
*/
|
||||
public function setBrand(string $brand): Toothbrush3
|
||||
{
|
||||
$this->brand = $brand;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
@ -63,4 +63,15 @@ class Entity1Repository extends ServiceEntityRepository
|
||||
->getOneOrNullResult()
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/schema-manager.html
|
||||
*
|
||||
* @return \Doctrine\DBAL\Schema\AbstractSchemaManager|null
|
||||
* @throws \Doctrine\DBAL\Exception
|
||||
*/
|
||||
public function getSchemaManager()
|
||||
{
|
||||
return $this->getEntityManager()->getConnection()->getSchemaManager();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user