dsfdfd1211 2018-06-20 08:40
浏览 52
已采纳

Symfony ManyToOne删除关系而不更新子实体

I have an entity Serveur which have a ManyToOne relation with Responsable

class Serveur implements NotifyPropertyChanged
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * @var \MyBundle\Entity\Responsable
     *
     * @ORM\ManyToOne(targetEntity="\MyBundle\Entity\Responsable", inversedBy="serveurs", cascade={"persist"})
     * @ORM\JoinColumn(name="serveur_responsable", referencedColumnName="id", nullable=true)
     *
     */
    private $responsable;

class Responsable
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="Uid", type="string", unique=true, nullable=false)
     */
    private $uid;

When you enter a new Responsable, in my updateAction (serveur), I call a service which look if there's already a Responsable with this uid, and If not I return a new Responsable :

public function updateAction(Request $request, $id)
{


    $entity = $em->getRepository('MyBundle:Serveur')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Serveur entity.');
    }

    $editForm = $this->createEditForm($entity);
    $editForm->handleRequest($request);

    if ($editForm->isValid()) {

        $resp = $editForm['responsable']->getData();

        if($resp->getUid()){
            $entity_resp = $this->container->get('app.responsable')->getResp($resp->getUid());
            $entity->setResponsable($entity_resp);
        }
        else{
            $entity->setResponsable(null);
        }

Now, when I edit a serveur, if there's no responsable and I enter one, it's working wether the responsable exist or not.

But If I edit a serveur which already have a responsable, and I want to setResponsable to null, it's not working :

An exception occurred while executing 'UPDATE responsable SET Uid = ? WHERE id = ?' with params [null, 192]: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'UNIQ_52520D076BD640E6'

It's actually trying to UPDATE the uid of the responsable to null, but that's not what I want, I'd like to keep the responsable in the DB and just remove the relation..

Am I missing something ?

Thanks !

  • 写回答

1条回答 默认 最新

  • dongshou9878 2018-06-26 12:09
    关注

    I used DataTransformer :

    class RespTransformer implements DataTransformerInterface{
    
        private $om;
    
        public function __construct(ObjectManager $om)
        {
            $this->om = $om;
        }
    
        public function transform($resp)
        {
            if(null === $resp){
                return '';
            }
    
            return $resp->getUid();
        }
    
        public function reverseTransform($uid)
        {
            if(!$uid){
                return;
            }
    
            $resp = $this->om
                ->getRepository('MyBundle:Responsable')
                ->findOneByUid($uid);
    
            if(null === $resp){
                $resp = new Responsable();
                $resp->setUid($uid);
            }
    
            return $resp;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计