我对多对多关系有一些严重的问题。在我的场景中, aCandidate
可以连接很多Projects
并且Project
可能有很多Candidates
.
CREATE TABLE `CANDIDATE` (
`ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`ID`) )ENGINE = InnoDB;
CREATE `PROJECT` (
`ID` INT NOT NULL AUTO_INCREMENT ,
PRIMARY KEY (`ID`)ENGINE = InnoDB;
CREATE TABLE `PROJECT_CANDIDATE` (
`CANDIDATE_ID` INT NULL ,
`PROJECT_ID` INT NULL ,
CONSTRAINT `FK_PROJECT_CANDIDATE`
FOREIGN KEY (`CANDIDATE_ID` )
REFERENCES `CANDIDATE` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
CONSTRAINT `FK_CANDIDATE_PROJECT`
FOREIGN KEY (`PROJECT_ID` )
REFERENCES `PROJECT` (`ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)ENGINE = InnoDB;
教义映射如下所示:
class Project
{
//.... other fields
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Candidate", mappedBy="projects", fetch="LAZY",cascade={"persist","merge"},orphanRemoval=true)
*/
private $candidates;
}
class Candidate
{
//.... other fields
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\ManyToMany(targetEntity="Project", inversedBy="candidates", fetch="LAZY",cascade={"persist","merge"},orphanRemoval=true)
* @ORM\JoinTable(name="project_candidate",
* joinColumns={
* @ORM\JoinColumn(name="CANDIDATE_ID", referencedColumnName="ID")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="PROJECT_ID", referencedColumnName="ID")
* }
* )
*/
private $projects;
}
我的问题是,为什么从这样的项目中删除候选人时:
//get $project with EntityManager
$project->candidates->clear();
表中的条目Candidates
也被删除,即使我有ON UPDATE/DELETE NO ACTION' for FK in
PROJECT_CANDIDATE`?