我有两个实体:工具和操作系统。两者都有联系。每个工具都可以在许多OS中运行。所以我通过多种关系将它们联系起来。 我可以添加一个工具并将其关联到许多操作系统,但我不知道如何删除工具及其与Oss的关系(这些关系被添加到我的数据库中的os_tool表中)。
工具实体:
use Lang\LanguageBundle\Form\ToolType;
namespace Lang\LanguageBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Tool
*/
class Tool
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $nombre;
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $oss;
/**
* Constructor
*/
public function __construct()
{
$this->oss = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add os
*
* @param \Lang\LanguageBundle\Entity\OS $os
* @return Tool
*/
public function addOs(\Lang\LanguageBundle\Entity\OS $os)
{
$os->addTool($this);
$this->oss[] = $os;
}
/**
* Remove os
*
* @param \Lang\LanguageBundle\Entity\OS $os
*/
public function removeOs(\Lang\LanguageBundle\Entity\OS $oss)
{
$this->oss->removeElement($oss);
}
/**
* Get oss
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getOss()
{
return $this->oss;
}
}
OS实体:
namespace Lang\LanguageBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* OS
*/
class OS
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $osName;
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $tools;
/**
* Constructor
*/
public function __construct()
{
$this->tools = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add tool
*
* @param \Lang\LanguageBundle\Entity\Tool $tool
* @return OS
*/
public function addTool(\Lang\LanguageBundle\Entity\Tool $tool)
{
$this->tools[] = $tool;
return $this;
}
/**
* Remove tools
*
* @param \Lang\LanguageBundle\Entity\Tool $tools
*/
public function removeTool(\Lang\LanguageBundle\Entity\Tool $tools)
{
$this->tools->removeElement($tools);
}
/**
* Get tools
*
* @return \Doctrine\Common\Collections\ArrayCollection
*/
public function getTools()
{
return $this->tools;
}
}
工具控制器:
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('LangLanguageBundle:Tool')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Tool entity.');
}
//$em->remove($entity);
//$em->flush();
}
return $this->redirect($this->generateUrl('tool'));
}
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('tool_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->getForm()
;
}
在Tool.orm.yml中我有以下代码:
manyToMany:
oss:
targetEntity: OS
mappedBy: tools
cascade: [ remove, persist ]
在OS.orm.yml中我有以下代码:
manyToMany:
tools:
targetEntity: Tool
inversedBy: oss
JoinTable:
name: tool_os
joinColumns:
JoinColumn:
name: os_id
referencedColumnName: id
inverseJoinColumns:
JoinColumn:
name: tool_id
referencedColumnName: id