Symfony2 MappedSuperClass and doctrine:generate:entities
Asked Answered
R

1

5

I have a "Offer" class (MapperSuperclass) and 2 more classes extending it, "PrivateOffer" and "PublicOffer".

The problem I have is, when I run "doctrine:generate:entities" command, both classes "PrivateOffer" and "PublicOffer" are fullfilled with the same properties than the MappedSuperclass "Offer" class, and also it's getter and setter methods.

If I remove them and live them only in the "Offer" class, the "doctrine:schema:update" works well as expected, but I need to run the "doctrine:generate:entities" again so, it ruins everytime my extended classes.

Why is the "doctrine:generate:entities" duplicating all the properties and setter/getter methods in both of the classes, if they extends the MappedSupperclass?

Thank you all :)

OfferClass:

namespace Pro\JobBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
/**
 * Offer
 *
 * @ORM\MappedSuperclass()
 */
class Offer
{
    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    protected $name;

    ....more properties...
}

PrivateOfferClass:

<?php

namespace Pro\JobBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * PrivateOffer
 *
 * @ORM\Table(name="private_offer")
 * @ORM\Entity
 */
class PrivateOffer extends Offer
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
}

PublicOfferClass:

<?php

namespace Pro\JobBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * PublicOffer
 *
 * @ORM\Table(name="public_offer")
 * @ORM\Entity
 */
class PublicOffer extends Offer
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
}
Row answered 9/9, 2014 at 16:12 Comment(0)
H
13

This is a known behaviour (not to say: bug) in Doctrine: In your scenario, all entity properties must be private. Accessing them must only be possible via the getters.

Helvetii answered 9/9, 2014 at 18:59 Comment(3)
Changed all properties from protected to private in the "Offer" class and problem solved. Thank you very much lxg!! :)Row
Can you please provide more details on this? Why is that so? :)Plosive
Basically, child classes can override public and protected properties. That's why they are not processed when a mapped superclass is evaluated. You can find a more detailed explanation at github.com/doctrine/mongodb-odm/issues/918 (it's for the Doctrine ODM but the same applies to the ORM).Merodach

© 2022 - 2024 — McMap. All rights reserved.