1

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.

David Kariuki
  • 1,522
  • 1
  • 15
  • 30
Viperium
  • 89
  • 8
  • 1
    Check out the link below for a similar example. [Similar Example Here](https://stackoverflow.com/a/11194717/3536236) – David Kariuki Apr 23 '19 at 10:42
  • 1
    @DavidKariuki Its interesting indeed but I am not using a abstract class to extend because the attributes are also a class that can be called. – Viperium Apr 23 '19 at 11:19

1 Answers1

2

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
{
   /*****/
}
Alexandre Tranchant
  • 4,426
  • 4
  • 43
  • 70
  • I tried removing : Products but it still gave the same error. I will try making it with the attributes Interface now. – Viperium Apr 24 '19 at 09:37
  • I edited my answer, please replace :Products by :Attributes (but it is still a best way to do it with attributes interface.) – Alexandre Tranchant Apr 24 '19 at 09:46
  • I tried using Attributes and also I just made a interface but the parentId and a other relation doesn't show in the database. – Viperium Apr 24 '19 at 10:48