2013-05-13 1 views
0

MySQL 테이블의 열을 "updated_at"이라고합니다. 날짜 형식이 아니고 null이 아닙니다. Doctine 유형도 datetime입니다. 그것이 내가이 열의 값을 가지고 싶지 않아 생성 된 후 실체 적 업데이트되지 않은 경우Doctrine으로 엔티티 업데이트 시간을 올바르게 설정하는 방법은 무엇입니까?

CREATE TABLE `example` (
    `id` int(10) unsigned NOT NULL AUTO_INCREMENT, 
    `created_at` datetime NOT NULL, 
    `updated_at` datetime NOT NULL, 
    PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

Some\Bundle\Entity\Example: 
    type:   entity 
    table:   examples 
    fields: 
     id: 
      id:  true 
      type: integer 
      generator: 
       strategy: IDENTITY 
     ... 
     createdAt: 
      type: datetime 
      column: created_at 
     updatedAt: 
      type: datetime 
      column: updated_at 

$e = new Example; 
$e->setCreatedAt(new \DateTime); 
$em->persist($e); 
/* after this I need $e->updatedAt to be MySQL's default 0 (0000-00-00) */ 
$em->flush(); 
/* but it will throw 
"Integrity constraint violation: 1048 Column 'updated_at' cannot be null" 
if I won't set it in Example::__contruct() 
which I don't see any reason to do 
apart from complying with Doctrine shortcomings. */ 

.

MySQL 또는 Doctrine 유형을 변경하지 않고이 열을 건너 뛰거나 적절한 값을 설정하는 방법 (올바른 것이므로)?

+0

DoctrineExtensions의 [Timestampable] (https://github.com/l3pp4rd/DoctrineExtensions/blob/master/doc/timestampable.md)을 사용하고 있습니까? –

+0

Symfony2.2를 doctrine ORM 및 doctrine 번들과 함께 사용합니다. – coviex

+0

예, 어떤 번들이 타임 스탬프 가능 동작을 제공합니까? [StofDoctrineExtensionsBundle] (https://github.com/stof/StofDoctrineExtensionsBundle)? –

답변

0

Doctrine은 열을 건너 뛸 수 없으며 메타 데이터 구성에서 정의한 모든 필드를 사용합니다.

생성자에서 일부 기본값을 설정해야합니다. 빈 문자열은 거의 모든 유형에 적합합니다.

지속 시마다 한 번만 사용되며 후속 가져 오기는 생성을 트리거하지 않습니다.

2

두 가지 옵션이 있습니다. 의견에서 언급 한대로 Timestampable을 사용하십시오. 심포니는 StofDoctrineExtensionsBundle에 통합되어 있습니다. 그런 다음 맵핑 yaml에 추가하기 만하면 컨트롤러, 서비스 등의 코드는 필요하지 않습니다.

옵션 2 : updated_at을 NOT NULL 대신 NULL로 설정하십시오. NOT NULL은 값이 있어야 함을 의미합니다 (예 : 0000-00-00). MySQL에서 NULL 값을 허용하면 고려해야 할 항목에 도달하게됩니다.

관련 문제