php - Doctrine 2 removes parent row

328

I have defined two database entities

Project:

use Doctrine\ORM\Mapping as ORM;
use \Kdyby\Doctrine\Entities\MagicAccessors;

/**
 * @ORM\Entity
 */
class Project extends \Kdyby\Doctrine\Entities\BaseEntity
{


    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    public $id;

    /**
     * @ORM\Column(type="string")
     */
    public $title;

    /**
     * @ORM\Column(type="text")
     * @var string
     */
    public $description;
    /**
     * @ORM\Column(type="datetime", nullable=true)
     * @var DateTime
     */
    public $created;

    /**
     * @ORM\Column(type="boolean")
     * @var string
     */
    public $public;

    /**
     * @ORM\Column(type="blob")
     * @var string
     */
    public $thumbnail;

    public function __construct()
    {
        $this->created = new \DateTime();
        $this->public = 0;
    }

}

and Personage:

use Doctrine\ORM\Mapping as ORM;
use \Kdyby\Doctrine\Entities\MagicAccessors;

/**
 * @ORM\Entity
 */
class Personage extends \Kdyby\Doctrine\Entities\BaseEntity
{


    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     */
    public $id;

    /**
     * @ORM\Column(type="string")
     */
    public $name;

    /**
     * @ORM\Column(type="text", nullable=true)
     * @var string
     */
    public $shortDescription;

    /**
     * @ORM\Column(type="text", nullable=true)
     * @var string
     */
    public $playerDescription;

    /**
     * @ORM\Column(type="datetime")
     * @var DateTime
     */
    public $created;

    /**
     * @ORM\Column(type="integer")
     * @var string
     */
    public $creator;


    /**
     * @ORM\Column(type="boolean", options={"default" = false})
     * @var boolean
     */
    public $inGraph;

    /**
     * @ORM\Column(type="boolean", nullable=false, options={"default" = 0})
     * @var boolean
     */
    public $deleted;

    /**
     * @ORM\ManyToOne(targetEntity="Project", cascade={"persist", "remove"})
     * @ORM\JoinColumn(name="project", referencedColumnName="id", onDelete="CASCADE")
     * @var string
     */
    public $project_id;

    public function __construct()
    {
        $this->deleted = 0;
        $this->inGraph = 0;
    }
}

Every Personage entity belongs to some project. But whenever I try to remove Personage either this way:

public function removeChar($id)
    {
        $a = $this->EntityManager->find('App\Personage', $id);

        if($a->deleted)
        {
            $this->EntityManager->remove($a);
            $this->EntityManager->flush();
        }

    }

or using dql. It removes the whole project row as well. What i want is whenever project is removed it removes all characters corresponding but NOT other way around.

I tried to remove

cascade={"persist", "remove"}

part from Project entity declaration (and updating database via console) but it said everything was in sync and did nothing. So I removed whole database and build it withoutcascade={"persist", "remove"}, but the problem was not solved.

130

Answer

Solution:

remove onDelete="CASCADE" from

@ORM\JoinColumn(name="project", referencedColumnName="id", onDelete="CASCADE")

and add

@ORM\OneToMany(targetEntity="Personage", mappedBy="project_id",cascade={"remove"})
private $personages;

in the project entity.

People are also looking for solutions to the problem: php - Create DB and include function

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.