php - One to One relation in Doctrine updates only one field - Integrity constraint violation: 1062 Duplicate entry -


it follow-up previous post relations while updating entity in symfony2 - one-to-one , one-to-many doesn't work

while i've managed solve issue one-to-many, although dirty fix in controller, same isn't working one-to-one, following error: sqlstate[23000]: integrity constraint violation: 1062 duplicate entry '16' key 'uniq_3bae0aa753c674ee'

the code:

    class offer { /**  * @var event  *  * @orm\onetoone(targetentity="event", inversedby="offer")  * @orm\joincolumn(name="event_id", referencedcolumnname="id")  */ private $event; }      class event { /**  * @var offer  *  * @orm\onetoone(targetentity="offer", mappedby="event")  * @orm\joincolumn(name="offer_id", referencedcolumnname="id", ondelete="set null")  */ private $offer; } 

plus, on last post has been suggested me add following code on setevent (precisely "$event->setoffer($this);" part:

/**  * set event  *  * @param \appbundle\entity\event $event  * @return offer  */ public function setevent(\appbundle\entity\event $event) {     $this->event = $event;     $event->setoffer($this);      return $this; } 

after update fills correct data event_id in offer table, or after tinkering code error duplicate entry sqlstate[23000]: integrity constraint violation: 1062 duplicate entry '16' key 'uniq_3bae0aa753c674ee'

any idea doing wrong? i'd grateful feedback.

update & solution (code added updateaction in offercontroller):

$oldevent = $em->getrepository('appbundle:event')->findoneby(array("offer"=>$entity));  if ($oldevent != null) {  $oldevent->removeoffer();   $em->persist($oldevent);   } $newevent = $entity->getevent();    $entity->setevent($newevent); 

not cleanset way, rather quick fix works fine, still im interested has happened doesn't work out-of-the-box in orm doctrine way.

in documentation can read that:

doctrine check owning side of association changes.

and here in detail:

to understand this, remember how bidirectional associations maintained in object world. there 2 references on each side of association , these 2 references both represent same association can change independently of 1 another. of course, in correct application semantics of bidirectional association maintained application developer (that’s responsibility). doctrine needs know of these 2 in-memory references 1 should persisted , not. owning/inverse concept used for.

your setevent method supposed do. takes care of inverse side of relationship.
means have manage entity owning side. offer owning side of relationship suggest calling persist on $offer instead of on $event.

$offer->setevent($event); $em->persist($offer); 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -