2016-08-18 55 views
3

使用PHP 7,Symfony 3.1.3和Doctrine 2.5.4。不跟蹤實體中某些字段變化的原則

我在嘗試將更新保存到實體。實體是另一個類的子類(簡單類的版本):

class ProjectRequest 
{ 
    // No Doctrine or Symfony annotations on the parent class 
    protected $title; 

    public function getTitle() 
    { 
     return $this->title; 
    } 

    public function setTitle($title) 
    { 
     $this->title = $title; 

     return $this; 
    } 
} 

class ProjectRequestNewProject extends ProjectRequest 
{ 
    /** 
    * @var string 
    * @ORM\Column(name="title", type="string") 
    * @Assert\NotBlank() 
    */ 
    protected $title; 

    /** 
    * @var string 
    * @ORM\Column(name="new_project_description", type="string") 
    * @Assert\NotBlank() 
    */ 
    protected $description; 

    /** 
    * @return string|null 
    */ 
    public function getDescription() 
    { 
     return $this->description; 
    } 

    /** 
    * @param string $description 
    * @return ProjectRequestNew 
    */ 
    public function setDescription(string $description): ProjectRequestNew 
    { 
     $this->description = $description; 

     return $this; 
    } 
} 

當我保存一個新的記錄,它工作正常(簡體):

$entity = new ProjectRequestNewProject(); 

//... Symfony form management 

$form->handleRequest(); 

$entityManager->persist($entity); 
$entityManager->flush(); 

當我嘗試更新實體然而,事情變得怪異。我的保存邏輯非常正常。我在多個其他項目(甚至同一個項目中的其他實體)上使用了相同的邏輯。

$projectRequest = $this->getDoctrine->getRepository('ProjectRequestNewProject')->find($id) 

$form = $this->createForm(
    ProjectRequestNewProjectType::class, 
    $projectRequest, 
    [ 
     'entityManager' => $entityManager, 
     'action' => $this->generateUrl('project_request_new_project_edit', ['id' => $id]) 
    ] 
); 

$form->handleRequest($request); 

if ($form->isSubmitted() && $form->isValid()) { 
    // save request 
    $entityManager->flush(); 

    // redirect to show project request 
    return $this->redirect(
     $this->generateUrl(
      'project_request_show', 
      ['id' => $id] 
     ) 
    ); 
} 

有了這個邏輯,如果我在實體更新$title場那麼學說更新按預期的方式記錄。但如果我只更改$description字段,Doctrine不會更新數據庫。

看看Symfony配置文件,我可以看到數據發送到服務器並正常轉換。看起來,Doctrine在確定實體是否發生了變化時,忽略了在子實體上聲明的字段的更改。

我找不到任何關於此行爲搜索谷歌或StackOverflow(我可能沒有使用正確的搜索條件),我不明白爲什麼教義是忽略對子實體上的字段的更改以確定它是否它需要更新數據庫。

同樣,如果我更改了這兩個標題和說明,那麼對兩個字段的更改都將保存到數據庫,但如果我只更改說明,則更改將不會保存到數據庫。

有沒有我在文檔中遺漏的東西,或者是否存在擴展基類的實體的問題?

回答