Doctrine 2删除父行

时间:2016-02-02 15:42:54

标签: php mysql doctrine-orm

我已经定义了两个数据库实体

项目:     

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;
    }

}

和人物:     

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;
    }
}

每个Personage实体都属于某个项目。但每当我尝试以这种方式删除Personage时:

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

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

    }

或使用dql。它也会删除整个项目行。我想要的是每当项目被删除时,它会删除所有相应的字符,但不是其他方式。

我试图删除

cascade={"persist", "remove"}

部分来自Project实体声明(并通过控制台更新数据库),但它说所有内容都是同步的,什么也没做。所以我删除了整个数据库并在没有cascade={"persist", "remove"}的情况下构建它,但问题没有解决。

1 个答案:

答案 0 :(得分:0)

删除onDelete =" CASCADE"来自

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

并添加

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

在项目实体中。