学说多对多协会实体:为什么删除XXX()不删除底层数据库记录?



我遇到需要向多对多联接表添加列的情况,因此我尝试遵循建议的做法,即让联接表由与其他两个实体中的每一个具有 ManyToOne 关系的实体表示。

在这种情况下,我们有一个法庭口译员管理系统,其中有一个名为 Event 的实体,另一个称为口译员的实体。InterpreterAssignment 实体对这两个实体都是一对多的,但它还需要两个元数据列:created日期时间和创建它的ApplicationEntityUser(为了简单起见,我省略了后者)。

所以,这工作得很好:

$interpreter = $entityManager->getRepository('ApplicationEntityInterpreter')
->findOneBy(['lastname'=>'Mintz']);
$assignment = new EntityInterpreterAssignment();
$assignment->setInterpreter($interpreter)->setEvent($event);
$event->addInterpretersAssigned($assignment);
$em->flush(); 

。我什至不需要说persist(),因为Event#interpretersAssignedcascade={"persist","remove"}).

但是,当我尝试做相反的事情时,即 使用教义为我写的removeInterpretersAssigned()方法:

$event = $entityManager->find('ApplicationEntityEvent',103510);
$assignment = $event->getInterpretersAssigned()[0];
$event->removeInterpretersAssigned($assignment);
$em->flush();

数据库保持不变;原则不会删除连接表中的行。

我可以通过说$entityManager->remove($assignment)来解决.但我忍不住认为$event->removeInterpretersAssigned($assignment)应该有效。

所以,我一定错过了什么,但我看不到什么。教义 cli 工具说我的映射没问题。以下是相关部分中的实体:

/* namespace declarations and use statements omitted */
class Event
{
/* other fields and methods omitted */
/**
* @ORMOneToMany(targetEntity="InterpreterAssignment",mappedBy="event",cascade={"persist","remove"})
* @var InterpreterAssignment[]
*/
protected $interpretersAssigned;
/*  the following created by the Doctrine cli tool  */
/**
* Remove interpretersAssigned
*
* @param ApplicationEntityInterpreterAssignment $interpretersAssigned
*/
public function removeInterpretersAssigned(ApplicationEntityInterpreterAssignment $interpretersAssigned)
{
$this->interpretersAssigned->removeElement($interpretersAssigned);
}
/**
* Get interpretersAssigned
*
* @return DoctrineCommonCollectionsCollection
*/
public function getInterpretersAssigned()
{
return $this->interpretersAssigned;
}
}
class Interpreter
{
/**
* @ORMOneToMany(targetEntity="InterpreterAssignment",mappedBy="interpreter")
* @var InterpreterAssignment[] 
*/
protected $assignments;
/**
* Remove assignment
*
* @param ApplicationEntityInterpreterAssignment $assignment
*/
public function removeAssignment(ApplicationEntityInterpreterAssignment $assignment)
{
$this->assignments->removeElement($assignment);
}
/**
* Get assignments
*
* @return DoctrineCommonCollectionsCollection
*/
public function getAssignments()
{
return $this->assignments;
}
}

这是InterpreterAssignment

/** 
* @ORMEntity 
* @ORMTable(name="interp_events", uniqueConstraints={@ORMUniqueConstraint(name="unique_deft_event",columns={"interp_id","event_id"})}) 
* @ORMHasLifeCycleCallbacks
*/
class InterpreterAssignment
{
/** 
* @ORMId
* @ORMManyToOne(targetEntity="Interpreter",inversedBy="assignments")
* @ORMJoinColumn(name="interp_id", referencedColumnName="interp_id")
* @var Interpreter
*/
protected $interpreter;
/**
* @ORMId
* @ORMManyToOne(targetEntity="Event",inversedBy="interpretersAssigned")
* @ORMJoinColumn(name="event_id", referencedColumnName="event_id")
* @var Event
*/
protected $event;

/**
* @ORMColumn(type="datetime",nullable=false)
* @var DateTime
*/
protected $created;
/**
* @ORMPrePersist
*/
public function onPrePersist()
{
$this->created = new DateTime();
}
/**
* Set interpreter
*
* @param ApplicationEntityInterpreter $interpreter
*
* @return InterpreterAssignment
*/
public function setInterpreter(ApplicationEntityInterpreter $interpreter)
{
$this->interpreter = $interpreter;
return $this;
}
/**
* Get interpreter
*
* @return ApplicationEntityInterpreter
*/
public function getInterpreter()
{
return $this->interpreter;
}
/**
* Set event
*
* @param ApplicationEntityEvent $event
*
* @return InterpreterAssignment
*/
public function setEvent(ApplicationEntityEvent $event)
{
$this->event = $event;
return $this;
}
/**
* Get event
*
* @return ApplicationEntityEvent
*/
public function getEvent()
{
return $this->event;
}
/* other stuff ommitted */
}

非常感谢。

我认为你需要做两件事:

  1. (可选)调用$assignment->setEvent(null)后需要呼叫$event->removeInterpretersAssigned($assignment);

  2. 此外,您可能希望使用孤立删除从多对多表中删除实体。 因此实体代码应更改为(请注意在映射代码中添加了, orphanRemoval=true):

    /**
    * @ORMOneToMany(targetEntity="InterpreterAssignment",mappedBy="event",cascade={"persist","remove"}, orphanRemoval=true)
    * @var InterpreterAssignment[]
    */
    protected $interpretersAssigned;
    

相关内容

  • 没有找到相关文章

最新更新