Symfony 2 + Doctrine 2 + Legacy

I am looking for a solution to the following database inheritance problem using Doctrine 2 built into the Symfony 2 framework. This is what I want to do ...

enter image description here

I want to create two tables (UredniHodiny, KonzultacniHodiny) with the same interface as the abstract Hodiny class. This is how i try to do it

<?php // src/CvutPWT/ImportBundle/Entity/Hodiny.php namespace CvutPWT\ImportBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\MappedSuperclass */ abstract class Hodiny { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\ManyToOne(targetEntity="Osoba") */ protected $osoba; /** * @ORM\ManyToOne(targetEntity="Mistnost") */ protected $mistnost; /** * @ORM\Column(type="datetime") */ protected $zacatek; /** * @ORM\Column(type="datetime") */ protected $konec; } <?php // src/CvutPWT/ImportBundle/Entity/KonzultacniHodiny.php namespace CvutPWT\ImportBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="konzultacnihodiny") */ class KonzultacniHodiny extends Hodiny { } <?php // src/CvutPWT/ImportBundle/Entity/UredniHodiny.php namespace CvutPWT\ImportBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="urednihodiny") */ class UredniHodiny extends Hodiny { } 

Now, when I run the php app/console doctrine:generate:entities CvutPWTImportBundle , Symfony generates all the variables (more precisely the columns) from the Hodiny class as private variables for both child classes. Now, when I try to create these tables with app/console doctrine:schema:update --force , I get errors that $id must be protected or weaker . When I change this protection manually, I can create tables, but there is only one column (id). But that is not what I was hoping for. Can someone give me some advice on what I'm doing wrong?

+6
source share
1 answer

This is not table inheritance. Displayed superclasses are just a display of inheritance. The tables corresponding to your final objects will not be related to each other.

If you want to get real table inheritance (a separate table or a joined table), use this: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/inheritance-mapping.html# single-table-inheritance

If you still want to use the mapped super classes, you will need to put the @ORM\Id definition in both final classes. You may not put identifiers in mapped superclasses.

+8
source

Source: https://habr.com/ru/post/914654/


All Articles