2012-02-25 4 views
2

symfony2로 작업하고 있습니다. 테이블이있어서 수동으로 ID를 할당하고 싶습니다. 교리의 문서에서Symfony2, 수동으로 테이블 id 값을 할당합니다.

내가 이걸 발견했습니다

식별자 발전 전략

이에
"NONE: Tells Doctrine that the identifiers are assigned (and thus generated) 
by your code. The assignment must take place before a new entity is passed to 
EntityManager#persist. 
NONE is the same as leaving off the @GeneratedValue entirely." 

에주의를 : "새 엔티티가 욕실에 전달되기 전에 할당이 이루어져야한다 .. "

어떻게 달성 할 수 있습니까?

나는이 일을 해요 :

$empleado->setId("989865446"); 

을하지만 말한다 :

Entity of type Entity\Empleado is missing an assigned ID. The identifier 
generation strategy for this entity requires the ID field to be populated before 
EntityManager#persist() is called. If you want automatically generated 
identifiers instead you need to adjust the metadata mapping accordingly. 

편집 :

/** 
* @var integer $id 
* @ORM\Column(name="id", type="integer", nullable=false) 
* @ORM\Id 
*/ 
private $id; 

public function setId($id) 
{ 
    $this->id = $id; 
} 
+1

당신이 우리에게 관련 코드를 표시 할 수 있습니다 : 코드는 (너무) 방식 내가 생성자가 필요로하는 ID와 어떻게 작동하는지 보여주는거야,하지만이 setId (함께 작동합니다 여기)과 같아야합니다 '$ empleado-> setId()'를 둘러싸고 있습니까? – Problematic

+0

어떻게'setId()'를 구현 했습니까? – greg0ire

+0

그리고 말할 필요도없이 (하지만 어쨌든 그것을 말할 것입니다) id 속성이 id라고 확인해야합니다. – Cerad

답변

2

게시 한 코드가 올바르므로 오류는 객체를 작성하는 방법과 persist()가 호출 된 시점과 관련이 있어야합니다.

비슷한 질문은 여기있다 : Set value for the primay key with strategy set to NONE

당신이/교리 2 심포니 2의 최신 버전을 실행하고 있습니까?

ID를 설정하기 전에 persist()를 호출 할 수 없도록 만드는 위의 대답에서 제안한 필수 생성자 매개 변수를 만들어보십시오. 오류 메시지는 그것이 무엇을 말하는지를 의미합니다. ID를 먼저 할당해야합니다. 다음으로 만 통화가 지속됩니다.

개체를 정상적으로 만들고 있습니까?

$empleado = new Entity\Empleado("989865446"); 
//set other properties 
$em->persist($empleado); 
+0

감사합니다. Matt. :) –

+0

setId 및 생성자를 사용하여 두 방법 모두 시도했습니다. id가 올바르게 설정되었지만 다음 자동 증가와 함께 유지됩니다. – afilina

+0

@afilina ID 속성에'@ GeneratedValue' 주석이 없는지 확인 했습니까? –

관련 문제