通过关系找到了新的实体?

A new entity was found through the relationship?

我有句柄码

if ($this->request->isMethod('POST') && $valid) {
            $em = $this->getDoctrine()->getManager();
            $formData = $form->getData();
            $staffValue = $formData['staff'];
            $campaignDetailFilter = $modelCampaignDetail->getRepository()->countDataByCampaignDetailId($formData['campaignDetail']);
            $total         = count($campaignDetailFilter);
            $totalStaff = count($formData['staff']);

            foreach ($campaignDetailFilter as $valDetailId) {
                $detailDataEntity = $modelDetailData->getEntity($valDetailId['id']);
                $batchSize = $total / $totalStaff;
                $i = 0;
                foreach ($staffValue as $staffVal) {
                    $detailDataEntity->setStaff($staffVal);
                    $em->persist($detailDataEntity);
                    if (($i % $batchSize) === 0) {
                        $em->flush();
                        $em->clear();
                    }
                    ++$i;
                }
                $em->flush();
                $em->clear();
            }
        }

但是当我给 $i = 0 时它得到一个错误:通过关系找到了一个新实体...没有配置为实体 的级联持久操作。

您应该删除 $em->clear()。

if (($i % $batchSize) === 0) {
     $em->flush();
}