Doctrine 2 - Access Level Issues When Using Table Class Inheritance

I'm trying to implement Table Class Inheritance Doctrine 2 offers in my project Symfony 2. Let's say you have the Pizza class, the Burito class and the MacAndCheese class, which all inherit from the Food class.

The Food class has the following settings:

<?php namespace Kitchen; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="food") * @ORM\InheritanceType("JOINED") * @ORM\DiscriminatorColumn(name="dish", type="string") * @ORM\DiscriminatorMap({"pizza" = "Pizza", "burito" = "Burito", "mac" => "MacAndCheese"}) */ class Food { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; 

And the inherited classes have the following settings ( Pizza ):

 <?php namespace Kitchen; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="food_pizza") */ class Pizza extends Food { 

When I start doctrine: schema: update -force from the Symfony 2 application / console, I get an error message about access to the $ id access level for children Food ( Pizza for example), stating that it should be protected or weaker. I did not declare $ id anywhere in Pizza , as I thought it would be inherited from Food .

So, I tried to declare $ id, but that gives me an error because I cannot override $ id. I suppose I need some kind of link to the $ id from Food to Pizza , but the Doctrine 2 documentation didn't really give me a clear answer on how this would look.

I hope you understand what I mean and can help me.

+7
source share
3 answers

Apparently, I should have examined the code created by the doctrine of: generate: entities a little more. When I started my IDE this morning and looked at the code again, I noticed that it “copied” all the inherited fields (for example, $ id in Food , in the above example) to children ( Pizza , in the example above).

For some reason, he decided to make these fields private. I manually changed the access level to protected in all classes, and I tried to run doctrine: schema: update --force again: it will work!

So, as in many cases, the decision was a good night's rest!;)

If someone comes up with a better solution and / or explanation for this problem, submit it. I would be more than happy to change the accepted answer.

+4
source

Something to keep in mind:

Each object must have an identifier / primary key. You cannot generate objects in the inheritance hierarchy at present (beta). As a workaround when creating methods for new objects, I moved the inherited objects away from the project, and after generation I moved them back.

a source

0
source

Maybe you should define @ORM \ DiscriminatorMap like this:

 /** * .. * @ORM\DiscriminatorMap({"food" = "Food", "pizza" = "Pizza", "burito" = "Burito", "mac" => "MacAndCheese"}) */ 

If you compare your code with the example from the Doctrine website, you will see that they added the parent to the DiscriminatorMap.

0
source

All Articles