2016-11-03 88 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

只有当我从清除方法中清除并且按照您的建议调用清除并清除时,这才起作用。但问题没有解决,因为我需要更新这些方法。 –