2014-07-11 63 views
0

我想创建一个API后,我有一个问题,当RestController做这一行:

$实体= $这个 - > getManager() - > createEntity();

它是如此的过程中返回NULL以有效的$形式的错误,我有一个错误500 而我直接在公共职能handleCreateRequest() 它的工作好,创建我的实体创建一个新的实体并放入我的数据库。 那么,为什么createEntity()返回NULL知道,这种方法是在ApiEntityManager.php等返回新的$这个 - >类

这是我的控制器POST方法:

/** 
* 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); 
} 

EDIT 1:

如果我做一个$ entity的var_dump():结果是: string'GroupeGC \ Bundle \ ProductBundle \ Entity \ Product'(length = 44)(所以它不为空) 那么为什么它不是0_o

+0

是您的getManager方法调用适当的服务? –

回答

1

工作你实现

/** 
* {@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_manager 
相关问题