2016-11-03 52 views
1

我搜索幾小時以找到解決此問題的解決方案,但沒有運氣。isset或empty中的非法偏移類型原理清除

也許這是重複的帖子,但我沒有找到它。

所以,我在的Symfony的服務問題,當我打電話實體管理器clear($entityname)方法我得到這個錯誤:

Warning: Illegal offset type in isset or empty. 

我不知道爲什麼發生這種情況。

如果我評論clear()方法,一切工作正常。

首先我打電話ProductController

public function postProductAction(Request $request) 
{ 
    if($jsonData = $this->requestToArray($request)){ 
     $productHandler = $this->get("admin.product_handler"); 
     $insertNews = $productHandler->insert($jsonData); 
     if($insertNews === true) { 
      return $this->jsonResponse->setData(array(
      "status" => true, "msg" => MessageConstants::SUCCESS_SEND_DATA)); 
     } 
    } 
    $this->jsonResponse->setStatusCode(204); 
    return $this->jsonResponse->setData(
    array("status" => false, "msg" => MessageConstants::FAILED_RECEIVE_RESPONSE)); 
} 

然後調用ProductHandler它集新產品

$productRepo = new Products(); 
$productRepo->setCarmodels(json_encode($data['ModelId'])); 
$productRepo->setCategoryid($category); 
$productRepo->setDescription($data['Description']); 
$productRepo->setDiscount($data['Discount']); 
$productRepo->setDiscountprice($data['DiscountPrice']); 
$this->em->persist($productRepo); 
$this->em->flush($productRepo); 
$insertOther = $this->update($productRepo->getId(), $data); 
$this->em->clear($productRepo); 

insertAnotfer呼叫更新,因爲有一些行動需要獲得產品ID,所以我首先需要插入然後做更新。

$product = $productRepo->find((int)$id); 
$product->setCarmodels(json_encode($data['ModelId'])); 
$product->setCategoryid($category); 
$product->setDescription($data['Description']); 
$product->setDiscount($data['Discount']); 
$product->setDiscountprice($data['DiscountPrice']); 

在更新我也呼籲明確方法

$this->em->flush($product); 
$this->em->clear($product); 

然後我得到這個錯誤。我試圖從更新中刪除清除方法,但沒有運氣。僅當我在插入函數中設置了沒有實體的clear()方法時纔會觸發錯誤。

+0

能否請編輯您的帖子,並顯示完整的代碼和發生問題。只顯示您清除實體的代碼/文件。謝謝! –

回答

1

實體管理::明確的方法有一個實體類型,而不是一個實體的實際實例的名稱:

\原則\ ORM \ EntityManager的

/** 
* Clears the EntityManager. All entities that are currently managed 
* by this EntityManager become detached. 
* 
* @param string|null $entityName if given, only entities of this type will get detached 
* 
* @return void 
*/ 
public function clear($entityName = null) 
{ 
    $this->unitOfWork->clear($entityName); 
} 

好像什麼您需要分離 - 將實體實例與實體管理器解除關聯的正確方法:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#detaching-entities

\原則\ ORM \ EntityManager的

/** 
* Detaches an entity from the EntityManager, causing a managed entity to 
* become detached. Unflushed changes made to the entity if any 
* (including removal of the entity), will not be synchronized to the database. 
* Entities which previously referenced the detached entity will continue to 
* reference it. 
* 
* @param object $entity The entity to detach. 
* 
* @return void 
* 
* @throws ORMInvalidArgumentException 
*/ 
public function detach($entity) 
{ 
    if (! is_object($entity)) { 
     throw ORMInvalidArgumentException::invalidObject('EntityManager#detach()' , $entity); 
    } 

    $this->unitOfWork->detach($entity); 
} 
0

我不相信你應該在flush或clear函數中指定參數。

你試過像這樣(沒有實體作爲參數):

$this->em->flush(); 
$this->em->clear(); 

我不能肯定這是否會爲你工作,但你可以嘗試一下,看看有沒有什麼幫助?雖然我可能是錯的,所以如果有人有更好的建議,請張貼。

+0

只有當我從清除方法中清除並且按照您的建議調用清除並清除時,這才起作用。但問題沒有解決,因爲我需要更新這些方法。 –

相關問題