1

Is it possible to have a self-referencing entity where the self-reference ID is NOT NULL? See my sample entity where parent.parent_id is configured with nullable=false. When I flush, I get the following error:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'parent_id' cannot be null (0) class: Doctrine\DBAL\Exception\NotNullConstraintViolationException

A trigger would work, however, I would rather not manually add a trigger. Could Doctrine be configured to generate a trigger? Maybe a lifecycle callback can be used?

<?php

use Doctrine\ORM\Mapping as ORM;

/**
 * ParentClass
 *
 * @ORM\Table(name="parent")
 * @ORM\Entity
 */
class ParentClass
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\OneToMany(targetEntity="ParentClass", mappedBy="parent")
     */
    private $child;

    /**
     * @var ParentClass
     *
     * @ORM\ManyToOne(targetEntity="ParentClass", inversedBy="child")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=false)
     * })
     */
    private $parent;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->child = new \Doctrine\Common\Collections\ArrayCollection();
    }

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

    /**
     * Add child.
     *
     * @param ParentClass $child
     *
     * @return ParentClass
     */
    public function addChild(ParentClass $child)
    {
        $this->child[] = $child;

        return $this;
    }

    /**
     * Remove child.
     *
     * @param ParentClass $child
     *
     * @return boolean TRUE if this collection contained the specified element, FALSE otherwise.
     */
    public function removeChild(ParentClass $child)
    {
        return $this->child->removeElement($child);
    }

    /**
     * Get child.
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getChild()
    {
        return $this->child;
    }

    /**
     * Set parent.
     *
     * @param ParentClass $parent
     *
     * @return ParentClass
     */
    public function setParent(ParentClass $parent)
    {
        $this->parent = $parent;

        return $this;
    }

    /**
     * Get parent.
     *
     * @return ParentClass
     */
    public function getParent()
    {
        return $this->parent;
    }
}
user1032531
  • 24,767
  • 68
  • 217
  • 387
  • For the first element that you will save in your table, you can put the parent as the element it self and in this case you avoid the null value. – KubiRoazhon Oct 21 '19 at 14:44
  • @KubiRoazhon How can MySQL save the record in the first place? Unless Doctrine will disable foreign constraints automatically when saving the first record which it appears not to. – user1032531 Oct 21 '19 at 15:26

1 Answers1

0

Try this. Hope it will work

public function addParent(ParentClass $parent)
{
    $this->parent[] = $parent;
    $parent->setParent($this);
    return $this;
}

public function removeParent(ParentClass $parent)
{
    $this->parent->removeElement($parent);
}

public function setParent()
{
    return $this->parent;
}

public function addParent(ParentClass $parent)
{
    $this->parent[] = $parent;
    $parent->setParent($this);
    return $this;
}

public function removeParent(ParentClass $parent)
{
    $this->parent->removeElement($parent);
}

public function getParent()
{
    return $this->parent;
}
Selim Reza
  • 683
  • 1
  • 11
  • 31