2011-10-05 2 views
1

모든 Doctrine2 설정은 YAML 파일 내에서 수행됩니다. 내가 prePersist 함수를 실행하려고하는 LoanAppMenuProgress라는 엔티티 클래스가 있습니다. 이 LoanAppMenuProgress 엔티티에는 LoanApp라는 다른 클래스와 oneToOne 관계가 있습니다. DB의 LoanApp 테이블과 연결된 LoanAppMenuProgress 테이블에 외래 키 연결이 있습니다.Doctrine2 lifecycleCallbacks prePersist가 YAML 설정을 사용하지 않음

나는 LoanApp.LoanAppMenuProgress.orm.yml 내 LoanAppMenuProgress 클래스에 대해이 설정을 내 LoanAppMenuProgress에서

LoanEv\LoanAppBundle\Entity\LoanApp\LoanApp: 
    type: entity 
    repositoryClass: LoanEv\LoanAppBundle\Repository\LoanApp\LoanAppRepository 
    table: loan_app 
    id: 
    id: 
     type: integer 
     generator: { strategy: auto } 

## This is the INVERSE side of the relationship. 
    oneToOne: 
    loanapp_menu: 
     targetEntity: LoanAppMenuProgress 
     mappedBy: loan_app 

    fields: 
    bank_id: 
     type: integer 
    # etc. 

: 이것은 내 LoanApp.LoanApp.orm.yml 파일

LoanEv\LoanAppBundle\Entity\LoanApp\LoanAppMenuProgress: 
    type: entity 
    repositoryClass: LoanEv\LoanAppBundle\Repository\LoanApp\LoanAppMenuProgress 
    table: loan_app_menu_progress 
    id: 
    id: 
     type: integer 
     generator: { strategy: auto } 

### This is the OWNING side of the relationship 
    oneToOne: 
    loan_app: 
     targetEntity: LoanApp 
     inversedBy: loanapp_menu 
     joinColumn: 
     name: loan_id 
     referencedColumnName: id 

    fields: 
    loan_id: 
     type: integer 
    menu_id2: 
     type: integer 
    menu_id3: 
     type: integer 
    menu_id4: 
     type: integer 

    lifecycleCallbacks: 
    prePersist: [ updateMainMenuStatus ] 

입니다 엔티티 클래스, 다음 코드가 있습니다 :

namespace LoanEv\LoanAppBundle\Entity\LoanApp; 

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Util\Debug; 

/** 
* LoanEv\LoanAppBundle\Entity\LoanApp\LoanAppMenuProgress 
*/ 
class LoanAppMenuProgress 
{ 
    private $id; 

    private $loan_id; 

    /** 
    * @var LoanEv\LoanAppBundle\Entity\LoanApp\LoanApp 
    */ 
    private $loan_app; 

    private $menu_id2 = 0; 

    private $menu_id3 = 0; 

    private $menu_id4 = 0; 

    // ... 

    public function updateMainMenuStatus() 
    {  
     echo("Inside prePersist's updateMainMenuStatus function. "); 
    } 
} 

다음 코드는 위트에서 호출됩니다. 나는 내 화면에 기록됩니다 다음 LoanAppController의 코드를 실행하면

// ... 

//Save the menuStatus changes. 
echo("About to persist. "); 
    $em->persist($menuStatus[0]); 
echo("Done persisting."); 
    $em->flush(); 

// ... 

: 지속될

"에 대해 내 LoanAppController 클래스를 힌. 완료 지속 "

I 출력 읽어야 중간에 그 비트 누락 :

". 소개 지속합니다. prePersist의 updateMainMenuStatus 함수 내부. 완료되었습니다. "

변경 사항이 데이터베이스에 쓰여지고 prePersist()를 제외하고 시스템의 모든 기능이 예상대로 작동합니다. 나는 꽤 오래 동안 yml 설정에 어려움을 겪었습니다. 내 초기 가정은 내 YAML 설정이 올바르지 않다는 것입니다.

문서에서는 (내가 이해할 수있는 한) lifecycleCallbacks : 및 prePersist : 항목을 yml 파일에 추가해야한다고 언급하고, 분명히 나는 ​​실종되었다.

아무도 아이디어가 없습니까?

감사합니다.

답변

5

prePersist는 INSERT 유형 명령문을 수행 할 때만 호출됩니다. 이 이벤트는 UPDATE 액션에서는 실행되지 않습니다. 엔티티가 업데이트 될 때 어떤 동작을 수행하려면 preUpdate를 사용하십시오. 그 preUpdate는 해당 엔티티로 수행 할 수있는 것에 훨씬 더 많은 제약이 있음에 유의하십시오.

데릭