【发布时间】:2022-09-23 21:10:07
【问题描述】:
我的用户代码:-
$projects = $this->doctrine->getRepository(Project::class)->findBy([\'deletionDate\' => new DateTime(\'today + 364 day\')]);
foreach($projects as $project){
$project = $this->entityManager->find(\'App\\Entity\\Project\', $project->getId());
$this->entityManager->remove($project);
}
$this->entityManager->flush();
这是错误:
An exception occurred while executing a query: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`foo`.`entry`, CONSTRAINT `FK_2B219D70166D1F9C` FOREIGN KEY (`project_id`) REFERENCES `project` (`id`))
这就是我正在尝试的: -
class Entry
{
/**
* @ORM\\ManyToOne(targetEntity=Project::class, inversedBy=\"entries\")
* @ORM\\JoinColumn(name=\"project_id\", referencedColumnName=\"id\", onDelete=\"CASCADE\")
*
* @Assert\\NotBlank
*/
public ?Project $project;
}
class Project
{
/**
* @ORM\\OneToMany(targetEntity=\"Entry\", mappedBy=\"project\", cascade={\"remove\"})
*/
public Collection $entries;
}