Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 + Doctrine 2 + inheritance

I'm searching for a solution for the following problem with a database inheritance using Doctrine 2 built in 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 class Hodiny. This is how I'm trying 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 php app/console doctrine:generate:entities CvutPWTImportBundle Symfony generates all variables (more precisely columns) from class Hodiny as private variables to both child classes. Now when I'm trying to create those tables with app/console doctrine:schema:update --force I'm getting errors that $id must be protected or weaker. When I change this protection manually I am able to create tables but there is only one column (id). But this is not what I was hoping for. Can somebody give me any advice what I'm doing wrong?

like image 660
Štěpán Heller Avatar asked May 01 '12 17:05

Štěpán Heller


1 Answers

This is not table inheritance. Mapped super classes are just mapping inheritance. The tables corresponding to your final entities will not be relied together in any way.

If you want real table inheritance (single table or 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 mapped super classes, then you will have to put the @ORM\Id definition in both final classes. You can not put ids in mapped super classes.

like image 144
Florian Klein Avatar answered Sep 21 '22 05:09

Florian Klein