在Symfony 2.8项目中,我有一个与CompanyProductionUnits有一对多关系的PersonDetail对象:
use Doctrine\ORM\Mapping as ORM,
Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* AppBundle\Entity\GeneralData\PersonDetail
*
* @ORM\Table(name="person_detail")
* @ORM\Entity(repositoryClass="PersonDetailRepository")
* @ORM\HasLifecycleCallbacks
*/
class PersonDetail
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var ArrayCollection $companyProductionUnits
*
* @ORM\OneToMany(targetEntity="PersonDetailCompanyProductionUnit", mappedBy = "personDetail", cascade={"persist", "remove"})
*/
private $companyProductionUnits;
}
<?php
namespace AppBundle\Entity\GeneralData;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* AppBundle\Entity\GeneralData\PersonDetailCompanyProductionUnit
*
* @ORM\Table(name="person_detail_company_production_unit")
* @ORM\Entity()
* @ORM\HasLifecycleCallbacks
*/
class PersonDetailCompanyProductionUnit {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var Company $company
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\GeneralData\CompanyProductionUnit")
* @ORM\JoinColumn(name="company_production_unit_id", referencedColumnName="id")
*/
protected $companyProductionUnit;
/**
* @var PersonDetail $personDetail
*
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\GeneralData\PersonDetail", inversedBy="companyProductionUnits")
* @ORM\JoinColumn(name="person_detail_id", referencedColumnName="id")
*/
protected $personDetail;
}
当用户在表单中执行特定操作时,我想清空公司单位数组集合,将moveEmployee值设置为1.因此,当moveEmployee的值为1时,我删除公司生产单元对象来自收藏。但是记录不会从数据库中删除。为什么呢?
这里是表单处理程序:
<?php
namespace AppBundle\Form\Handler;
use AppBundle\Entity\GeneralData\PersonDetailCompanyProductionUnit;
use Symfony\Component\HttpFoundation\Request,
Symfony\Component\Form\FormInterface,
Symfony\Component\HttpFoundation\Session\Session;
use Doctrine\ORM\EntityManager;
use AppBundle\Entity\GeneralData\PersonDetail;
class CompanyProductionUnitEmployeeFormHandler {
private $entityManager;
private $session;
public function __construct(
EntityManager $entityManager,
Session $session
)
{
$this->entityManager = $entityManager;
$this->session = $session;
}
public function handle(FormInterface $form, Request $request, $message)
{
if(!$request->isMethod('POST')) {
return false;
}
$form->bind($request);
if(!$form->isValid()) {
return false;
}
$data = $request->request->get('company_production_unit_employee');
$productionUnit = $this->entityManager
->getRepository('AppBundle\Entity\GeneralData\CompanyProductionUnit')
->find($data['productionUnit']);
$personDetail = $this->entityManager
->getRepository('AppBundle\Entity\GeneralData\PersonDetail')
->find($data['personDetail']);
if($data['moveEmployee'] == 1) {
//first try
foreach($personDetail->getCompanyProductionUnits() as $unit) {
$this->entityManager->remove($unit);
}
//I tried also in the following way but without success
//$personDetail->companyProductionUnits->clear();
}
$personDetailCompanyProductionUnit = new PersonDetailCompanyProductionUnit();
$personDetailCompanyProductionUnit->setPersonDetail($personDetail);
$personDetail->addCompanyProductionUnit($personDetailCompanyProductionUnit);
$this->persist($personDetail, $message);
return true;
}
public function persist(PersonDetail $personDetail, $message)
{
$this->entityManager->persist($personDetail);
$this->entityManager->flush();
$this->session->getFlashBag()->add('success', $message);
}
}
答案 0 :(得分:0)
我快速浏览了一下并认为这可能是最有帮助的,从您的要求来看,我认为这份文件可能会帮助您解决这个问题。 Working with associations
答案 1 :(得分:0)
我随便发现,当我应用我在前一个示例中尝试的两种策略时,我的代码都能正常工作。 因此,解决方案在表单处理程序中使用以下代码:
if($data['moveEmployee'] == 1) {
foreach($personDetail->getCompanyProductionUnits() as $unit) {
$this->entityManager->remove($unit);
}
$personDetail->companyProductionUnits->clear();
}