2013-08-06 5 views
1

symfony + Doctrine을 사용하고 있는데 문제가 있습니다.
기존 개체를 복제했으며 복제본에서 FK를 변경하고 싶습니다. 그것과 같아야합니다 :외래 키가 복제되지 않은 상태에서 symfony2가 복제 된 후

$dafCloned = clone $daf; 
$dafState = $dafStateRepository->findOneBy(
     array(
      'name' => 'saved', 
      'dafType' => 'invoice', 
      'company' => $daf->getSeller(), 
     )); 
$dafCloned->setDafState($dafState); 
var_dump($dafState->getId()); 
var_dump($dafCloned->getDafState()->getId()); 
$this->em->persist($dafCloned); 
$this->em->flush(); 

당신이 눈치 챘을지도 모르겠지만 여기에 2 개의 var_dump가 있습니다.

int(5500) 
int(5499) 

5500

내가 $ dafCloned, 5499에 대한 DB를 가지고 있어야 ID입니다 나는 $ DAF에 대해 가지고있는 ID입니다 : 여기에이 코드를 호출하는 사용자 지정 명령의 인쇄입니다.
다른 ID를 가진 이유는 무엇입니까? 내 dafState는 동일해야합니다. 나는 아마 바보 같은 것을 놓쳤을 것이다. 그러나 나는 심지어 오전 9시 이후로 붙어있다 ... 나는 심지어 우리가 가진 모든 캐시를 지우려고했는데, flush()persist()하지만 움직이지 않는다. :

EDIT : setDafState 필요하지만, 이것은 기본적인 방법 경우 :

public function setDafState(DafState $dafState) { 

    $this->dafState = $dafState; 

    return $this; 
} 

EDIT2 : 여기 getDafState() : 더 코드 샘플이 필요하면

/** 
* Get dafState 
* 
* @return MyPath\Entity\DafState 
*/ 
public function getDafState() { 
    return $this->dafState; 
} 

, 그냥 편집 할 수 있습니다, 그것을 요청)

개체의 경우 모두 거대 (Doctrine Object)이며 유용 할 수있는 방법을 찾을 수 없습니다. $ daf Object에 grep dafState를 사용할 수 없습니다. 출력은 여전히 ​​엄청납니다.

편집 3 :

if ($daf->getId() == 8902) // daf test which should be duplicated 
       var_dump($dafCloned->getDafState() === $dafState); 

출력

bool(true) 
+0

두 경우 모두 전체'DafState' 객체를'var_dump '할 수 있습니까? 'getDafState()'정의를 추가 할 수 있습니까? – cheesemacfly

+0

수정 된 질문이 정의를 추가합니다. 질문에 설명 된 것처럼 객체를 추가 할 수 없습니다. –

+0

그리고'var_dump ($ dafCloned-> getDafState() === $ dafState)'하면'true' 또는'false'를 얻게됩니까? – cheesemacfly

답변

0

여기 있습니다.

@cheesemacfly 덕분에 나는 내 dafState를 재설정하는 prePersistListener를 찾았습니다!

다음 번에 위와 같은 문제가 생기면 다음을 참조하십시오.

0
$dafCloned = clone $daf; // Here your clone is the same object as the old one 
$dafState = $dafStateRepository->findOneBy(// Here you get some fresh object 
       array(
         'name' => 'saved', 
         'dafType' => 'invoice', 
         'company' => $daf->getSeller(), 
       )); 
$dafCloned->setDafState($dafState); // Because this object is still managed by the entity manager it will set the $dafState on the old object (tracked by Id most likely) 
var_dump($dafState->getId()); // Show the Id on the fresh object 
var_dump($dafCloned->getDafState()->getId()); // Show the Id on the old object 
$this->em->persist($dafCloned); // overwrite the old object 
$this->em->flush(); 

이 게시물에 당신에게 도움이 될 것입니다 :이 문제가 해결되지 않을 경우 How to re-save the entity as another row in Doctrine 2 나는 내 대답을 업데이트합니다

+0

글쎄, 내가이 경우 틀리지 않다면 var_dump ($ daf-> getDafState() -> getId()); 은 나에게 동일하게 줄 것입니다. var_dump ($ dafState-> getId()); ? –