Viperium
Viperium

Reputation: 89

How to extend entity with internal relation?

I hope you will be able to help because I have been searching for a few days now. I have a class called "Attributes". In it, there is a parentId which reference a other entry of Attributes.

I want to extend the entity "Attributes" as "Products" with a few extra fields. All the fields are being extended except for the parentId relation.

When I add parent to Products with getter and setter I get a error:

`"Compile Error: Declaration of Products::setParent(?Attributes $parent): Products must be compatible with Attributes::setParent(?Attributes $parent)
  : Attributes"`

I have tried a standalone Entity Products with all the fields but the relationship to Attributes causes database relation issues. And it is an extension of Attributes.

Tried different kinds of getters and setters and leaving it out to be extended from the parent.

Have been searching the web for a answer but have not seen inheritance with a internal relationship.

Attributes class:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class Attributes
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="string", length=255)
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $val;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $category;

    /**
     * @ORM\ManyToOne(targetEntity="App\Entity\Attributes")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
     */
    private $parent;
}

Products class:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class Products extends Attributes
{
   /**
     * @ORM\Column(type="string", length=255)
     */
    private $newField1;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $newField2;
}

I would like to know why the internal relationship "Parent" isn't extended and I would love to know how to get the parentId in the extended class.

Upvotes: 1

Views: 118

Answers (1)

Alexandre Tranchant
Alexandre Tranchant

Reputation: 4551

Your parent setters seems to be like this:

/** Product **/
public function setParent(?Attributes $parent): Products
{
   $this->parent = $parent;

   return $this;
}
/** Attribute **/
public function setParent(?Attributes $parent)
{
   $this->parent = $parent;

   return $this;
}

You can fix it simply by replacing :Products by :Attributesat the end of setParent line in product class.

But you should use an interface

interface AttributeInterface 
{
    public function setParent(?AttributeInterface $parent): AttributeInterface
}

and each of your entity should implement it:

Attribute:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class Attributes implements AttributesInterface
{
    /*****/
    public function setParent(?AttributeInterface $parent): AttributeInterface
    {
        $this->parent = $parent;

        return $this;
    }
}

Product:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 */
class Products extends Attributes implements AttributesInterface
{
   /*****/
}

Upvotes: 2

Related Questions