无法在一对多关系中从数据库中删除记录



在Symfony 2.8项目中,我有一个与CompanyProductionunits有一对多关系的Persondail对象:

use DoctrineORMMapping as ORM,
    DoctrineCommonCollectionsArrayCollection;
use GedmoMappingAnnotation as Gedmo;
/**
 * AppBundleEntityGeneralDataPersonDetail
 *
 * @ORMTable(name="person_detail")
 * @ORMEntity(repositoryClass="PersonDetailRepository")
 * @ORMHasLifecycleCallbacks
 */
class PersonDetail
{
    /**
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var ArrayCollection $companyProductionUnits
     *
     * @ORMOneToMany(targetEntity="PersonDetailCompanyProductionUnit", mappedBy = "personDetail", cascade={"persist", "remove"})
     */
    private $companyProductionUnits;
}

<?php
namespace AppBundleEntityGeneralData;
use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo;
/**
 * AppBundleEntityGeneralDataPersonDetailCompanyProductionUnit
 *
 * @ORMTable(name="person_detail_company_production_unit")
 * @ORMEntity()
 * @ORMHasLifecycleCallbacks
 */
class PersonDetailCompanyProductionUnit {
    /**
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;
    /**
     * @var Company $company
     *
     * @ORMManyToOne(targetEntity="AppBundleEntityGeneralDataCompanyProductionUnit")
     * @ORMJoinColumn(name="company_production_unit_id", referencedColumnName="id")
     */
    protected $companyProductionUnit;
    /**
     * @var PersonDetail $personDetail
     *
     * @ORMManyToOne(targetEntity="AppBundleEntityGeneralDataPersonDetail", inversedBy="companyProductionUnits")
     * @ORMJoinColumn(name="person_detail_id", referencedColumnName="id")
     */
    protected $personDetail;
}    

我想在用户以表单上执行特定操作时将公司单位数组集合清空,将MoveEmployee值设置为1。因此,当MoveEmployee的值为1时,我删除公司生产单位对象从收藏中。但是该记录并未从数据库中删除。为什么?

在这里您是形式处理程序:

<?php
namespace AppBundleFormHandler;
use AppBundleEntityGeneralDataPersonDetailCompanyProductionUnit;
use SymfonyComponentHttpFoundationRequest,
    SymfonyComponentFormFormInterface,
    SymfonyComponentHttpFoundationSessionSession;
use DoctrineORMEntityManager;
use AppBundleEntityGeneralDataPersonDetail;
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('AppBundleEntityGeneralDataCompanyProductionUnit')
            ->find($data['productionUnit']);
        $personDetail = $this->entityManager
            ->getRepository('AppBundleEntityGeneralDataPersonDetail')
            ->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);
    }
}

我很快地环顾四周,认为这可能是最有用的,从您的问来看,我认为本文档可能会帮助您弄清楚它。与协会合作

我随便发现我的代码在应用上一个示例中尝试的两种策略时都起作用。结果,该解决方案是在表单处理程序中使用以下代码:

if($data['moveEmployee'] == 1) {
    foreach($personDetail->getCompanyProductionUnits() as $unit) {
        $this->entityManager->remove($unit);
    }
    $personDetail->companyProductionUnits->clear();
}

相关内容

  • 没有找到相关文章