我想创建一个post API,当RestController执行这一行时,我遇到了一个问题:
$this->getManager()->createEntity(); $entity =
它返回的是Null,所以进程要使$form错误生效,而我有一个错误500。虽然我直接在公共函数handleCreateRequest()中创建了一个新实体--它工作得很好--但是我的实体被创建并放入我的数据库中。那么,为什么createEntity()返回null,知道此方法在ApiEntityManager.php et返回新$ 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:
如果我做了一个var_dump()的$entity :结果是:字符串$entity (length=44) (所以它不是空的),那么为什么它不能工作0_o
发布于 2014-08-12 09:12:58
你有没有实施
/**
* {@inheritdoc}
*/
public function getManager()
{
return $this->get('your_manager_service_name');
}在你的控制器里?
your_manager_service_name应该在services.yml中声明,如下所示:
your_manager_service_name:
class: %your_manager_service_name.class%
arguments:
- %your.entity.class%
- @doctrine.orm.entity_managerhttps://stackoverflow.com/questions/24694969
复制相似问题