我搜索了几个小时来找到这个问题的解决方案,但没有运气。
也许这是重复的帖子,但我找不到它。
所以,我在 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']);
在更新中我也称为clear方法
$this->em->flush($product);
$this->em->clear($product);
然后我收到此错误。我试图从更新中删除明确的方法,但没有运气。仅当我在插入函数中设置了没有实体的clear()
方法时,才会触发错误。
答案 0 :(得分:1)
EntityManager :: clear方法采用实体类型的名称,而不是实体的实际实例:
<强> \学说\ 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);
}
看起来你需要的是分离 - 将实体实例与实体管理器解除关联的正确方法:
<强> \学说\ 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);
}
答案 1 :(得分:0)
我不相信你应该在flush或clear函数中指定参数。
您是否尝试过这样做(没有实体作为参数):
$this->em->flush();
$this->em->clear();
我不确定这是否对您有用,但您可以尝试一下,看看是否有帮助吗?我可能错了,所以如果有人有更好的建议,请发帖。