2017-09-12 71 views
0

嗨我有一個表中的任務在我的laravel應用程序。我想從今天起刪除兩天以外的任務。刪除日期與今天兩天的差異 - 學說,QueryBuilder

我已經writtern功能:

public function deleteOldTasks(){ 

     $results = $this->entityManager->createQueryBuilder() 
       ->select('cc')->from('\TodoList\Http\Entities\Task', 'cc') 
       ->getQuery() 
       ->getResult(); 

     $today = Carbon::today(); 

     foreach ($results as $result){ 


      $tempDate = $result->getCreatedAt()->format('Y-m-d H:i:s'); 
      $datework = new Carbon($tempDate); 
      $diff = $datework->diffInDays($today); 
      if($diff >= 2){ 

       $this->entityManager->persist($result); 
      } 
     } 
     $this->entityManager->flush(); 


    } 

該解決方案這麼想的工作。這是正確的刪除對象的方式?我的意思是在使用查詢生成器進行選擇後刪除,使用方法persist和flush從表中刪除每個簡單記錄?這不適用於我的代碼。我會很樂意幫忙的。最好的問候;)

回答

1

你的代碼中沒有刪除方法。在你的flush之前,嘗試添加$ this-> entityManager-> remove($ result);