1
votes

I got a ParentClass like this

/** @ORM\MappedSuperclass */
class BaseValue
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var field
     * @ORM\OneToMany(targetEntity="Field", mappedBy="value", cascade="all")
     */
    protected $field;

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

    /**
     * @param int $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return field
     */
    public function getField()
    {
        return $this->field;
    }

    /**
     * @param field $field
     */
    public function setField($field)
    {
        $this->field = $field;
    }
}

And a child like this

 * @ORM\Entity
 * @ORM\Table(name="integers")
 */
class Integer extends BaseValue
{
    /**
     * @var integer
     *
     * @ORM\Column(name="value", type="integer", nullable=true)
     */
    protected $value;

    /**
     * @return string
     */
    public function getValue()
    {
        return $this->value;
    }

    /**
     * @param string $value
     */
    public function setValue($value)
    {
        $this->value = $value;
    }

}

Now I would like to relate the child in another class like this

* @ORM\Entity
 * @ORM\Table(name="fields")
 */
class Field
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var
     * @ORM\ManyToOne(targetEntity="BaseValue", mappedBy="field", cascade="all")
     * @ORM\JoinColumn(name="vid", referencedColumnName="id")
     */
    protected $value; // but it does not work

It always gets me the following error:

[Doctrine\Common\Annotations\AnnotationException]
[Creation Error] The annotation @ORM\ManyToOne declared on property zmpim\Entity\Field::$value does not have a property named "mappedBy". Available properties: targetEntity, cascade, fetch, inversedBy

Both got mappedby,.. so the error seems to be senseless


Update:

A field has got values and labels. The values get inherited from BaseValue into IntegerValue, StringValue and later others...

My OneToMany Relation is a parent class of inheritance. like this, now:

/** @ORM\MappedSuperclass */
class BaseValue
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var Field
     * @ORM\OneToMany(targetEntity="Field", mappedBy="field", cascade="persist", orphanRemoval=true )
     */
    protected $field;

And this is my ManyToOne:

/**
 * 
 * @ORM\Entity
 * @ORM\Table(name="fields")
 */
class Field
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\Column(name="id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var int|null
     * @ORM\ManyToOne(targetEntity="BaseValue", inversedBy="value")
     * @ORM\JoinColumn(name="vid", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $value;

It still give me an error, but now it is:

[Doctrine\ORM\ORMException]
Column name id referenced for relation from zmpim\Entity\Field towards zmpim\Entity\BaseValue does not exist.

1

1 Answers

4
votes

Your Entity Field is the inversed side of your mapping so instead of using MappedBy declaration you have to use this

 /**
     * Inversed side
     * @var int|null
     * @ORM\ManyToOne(targetEntity="BaseValue", inversedBy="field")
     * @ORM\JoinColumn(name="[your_name]", referencedColumnName="[id]", onDelete="CASCADE")
     */
    protected $value;

To understand well inversedSide and MappedBy attributes you can read this: Doctrine inverse and owning side

After reading again you are aware of your relationnal problem between the two of your entities, but if you declare ManyToOne annotation, you have to set inversedBy attribute or you'll get an error. And this is what you have. You can't declare ManyToOne annotation with mappedBy attribute because it does not exist and throw an exception by Doctrine.

To resume :

ManyToOne association =>

* @ORM\ManyToOne(targetEntity="[yourEntity]", inversedBy="[Field]")

Be careful this side require the declaration of this :

 * @ORM\JoinColumn(name="[your_name]", referencedColumnName="[id]", onDelete="CASCADE")

OneToMany =>

* @ORM\OneToMany(targetEntity="[An_Entity]",
     * mappedBy="[Field]", cascade={"persist"}, orphanRemoval=true)

EDIT from your answer : Your mapping is still incorrect, your data in InversedBy And mappedBy need to be switched.