2013-04-08 1 views
1

다음과 같은 상황이 있습니다 : DataObject 클래스를 확장하는 (Doctrine Entity) ContentCategory. 데이터 객체 클래스는 onPrePersist, 다음과 같은 기능을 가지고 :Doctrine ORM Annotation으로 함수 확장하기

/** 
* @ORM\HasLifecycleCallbacks 
*/ 
class DataObject implements InputFilterAwareInterface 
{ 
... 
/** @ORM\PrePersist */ 
public function onPrePersist() 
{ 
    //using Doctrine DateTime here 
    $this->creation_date = new \DateTime('now'); 
} 

ContentCategory 클래스는이 기능 aswell이 필요합니다. 이 함수를 ContentCategory 클래스에 넣으면 잘 작동합니다. ContentCategory 클래스가 onPrePersist()와 같은 함수를 클래스 자체에서 정의하지 않고 사용할 수있는 방법이 있습니까?

* @ORM\HasLifecycleCallbacks() 
*/ 
class ContentCategory extends DataObject implements InputFilterAwareInterface 
{ 
... 
} 

개체에게 onPrePersist 기능을 제공하는 이유는,이 객체가 생성 된 날짜 시간 또는 데이터 객체 클래스를 확장되고 다른 객체/엔티티를 설정하는 것이다.

-< 편집> - 내가 현재이 같은 ContentCategory에 구조 방법을 추가 한

: 새 엔티티를 만들 때

public function __construct() { 
    parent::onPrePersist(); 
} 

이 방법의 교리는 함수 onPersist을 실행 . 다른 하나는 enttiy가 Doctrine으로 업데이트 될 때입니다. Modified_date를 설정하고 싶습니다. 이 경우 DataObject 클래스에 이와 같은 함수가 있습니다.

/** 
* @ORM\HasLifecycleCallbacks 
*/ 
class DataObject implements InputFilterAwareInterface 
{ 
... 
/** 
* @ORM\PreUpdate 
*/ 
public function onUpdate() 
{ 
    $this->last_modified_date = new \DateTime('now'); 
} 

추가 된 교리 ORM 주석 (PreUpdate)는 함수가 (위) 객체에 대한 업데이 트 문에 excuted됩니다 있는지 확인합니다. 문제는 DataObject를 확장하는 객체에서 해당 함수를 호출하는 방법입니다.

답변

1
/** 
* @ORM\MappedSuperclass 
* @ORM\HasLifecycleCallbacks 
*/ 
class TestimonialSuperclass 
{ 
    /** 
    * @ORM\PreFlush 
    */ 
    public function onPreFlush() 
    { 
     echo 123; 
    } 
} 


/** 
* @ORM\Entity 
* @ORM\Table(name="testimonials") 
* @ORM\HasLifecycleCallbacks 
*/ 
class Testimonial extends TestimonialSuperclass 
{ 
    ... 
} 
+0

감사합니다. Doctrine ORM Annotation : MappedSuperclass에 대해 알지 못했습니다. 이 주석으로 수퍼 (부모) 클래스의 엔티티를 만들지 않습니다. 고마워, 내 눈을 열어! – Kwido

관련 문제