我想创建一个帖子 API,当 RestController 执行此行时我遇到了问题:
$entity = $this->getManager()->createEntity();
它返回 Null,因此验证 $form 错误的过程,我有一个错误 500。虽然我直接在公共函数 handleCreateRequest()中创建了一个新实体, 但它运行良好,我的实体被创建并放入我的数据库中。那么为什么 createEntity() 返回 null 知道此方法在 ApiEntityManager.php 中并返回new $this->class
这是我的控制器发布方法:
/**
* REST POST Product
*
* @ApiDoc(
* section="Product",
* description="Post product item",
* resource=true
* )
* @return \Symfony\Component\HttpFoundation\Response
*/
public function postAction()
{
return $this->handleCreateRequest();
}
这是handleCreateRequest()
/**
* Create new
*
* @return Response
*/
public function handleCreateRequest()
{
$entity = $this->getManager()->createEntity(); <---- this line return null
-------------------------------------------------------------------------
// $entity = new \GroupeGC\Bundle\ProductBundle\Entity\Product(); |
// $entity->setCode( $this->getRequest()->get("code")); |
// $entity->setLabel( $this->getRequest()->get("label")); |
-------------------------------------------------------------------------
//this part of code work well but it's not generic
$isProcessed = $this->processForm($entity);
if ($isProcessed) {
$entityClass = ClassUtils::getRealClass($entity);
$classMetadata = $this->getManager()->getObjectManager()->getClassMetadata($entityClass);
$view = $this->view($classMetadata->getIdentifierValues($entity), Codes::HTTP_CREATED);
} else {
$view = $this->view($this->getForm(), Codes::HTTP_BAD_REQUEST);
}
return $this->handleView($view);
}
编辑 1:
如果我执行 $entity 的 var_dump() :结果是: 字符串 'GroupeGC\Bundle\ProductBundle\Entity\Product' (length=44)(所以它不为空)所以为什么它不起作用 0_o