2016-07-04 4 views
0

통합 테스트를하고 있으며 엔티티에 ID를 직접 설정할 수 없습니다. 내가 필요로하는 것은 어떻게 든 Doctrine에게 ID를 1로 재설정하도록 지시합니다.Doctrine 2 재설정 ID

나는 플러시를 호출 할 때 ID 20, 21, 22,23을 얻습니다.

테이블을 삭제하려고 시도했으나 신원을 재설정하거나이 thread은 도움이되지 않습니다.

내 코드 : 사전에

public function testFindingAllOrderedByDefault() 
{ 
    /** @var $bundles Bundle[] */ 
    $bundles = $this->repository->findAll(); 
    $this->assertCount(2, $bundles); 
    $this->assertSame(1, $bundles[0]->getId()); 
    $this->assertSame(2, $bundles[1]->getId()); 
} 

protected function prepareDatabase(EntityManager $entityManager, Connection $connection) 
{ 
    $connection->executeQuery('TRUNCATE bundle CASCADE'); 
    $entityManager->persist(
     (new Bundle()) 
      ->setPrice(1200) 
      ->setPriceVat(5000) 
      ->setDiscount(1000) 
      ->setCurrency('czk') 
    ); 
    $entityManager->persist(
     (new Bundle()) 
      ->setPrice(1300) 
      ->setPriceVat(4000) 
      ->setDiscount(500) 
      ->setCurrency('eur') 
    ); 
    $entityManager->flush(); 
} 

감사합니다.

답변

1

여기서 문제는 교리와 관련이 없습니다. MySQL은 테이블 메타 데이터에 저장된 다음 사용 가능한 기본 키를 유지합니다. 당신은 를 "재설정"또는 ALTER TABLE 쿼리를 실행하여 값을 변경할 수 있습니다 : 교리 마이그레이션에

ALTER TABLE your_table AUTO_INCREMENT = 1; 

을 또는 :

public function up(Schema $schema) 
{ 
    $this->connection->exec('ALTER TABLE your_table AUTO_INCREMENT = 1'); 
} 

이 교리 마이그레이션 번들을 사용하지 않는 경우 일회용 스크립트를 작성하고 엔티티 관리자를 사용할 수 있습니다.

$em->getConnection()->exec('ALTER TABLE your_tbl AUTO_INCREMENT = 1'); 
+0

고마워, 저는 Postgres를 사용하고 있으므로 $ connection-> executeQuery ('ALTER SEQUENCE bund le_id_seq RESTART WITH 1 '); –