0

I want to create a post API , and i have a problem when the RestController do this line :

$entity = $this->getManager()->createEntity();

It's return Null so the process to valid the $form bug and i have a error 500. Whereas i create a new entity directly in the public function handleCreateRequest() it's work well , my entity is created and put in my database. So why the createEntity() return null knowing that this method is in ApiEntityManager.php et return new $this->class

this is my Controller Post Method :

 /**
* REST POST Product
*
* @ApiDoc(
*      section="Product",
*      description="Post product item",
*      resource=true
* )
* @return \Symfony\Component\HttpFoundation\Response
*/
public function postAction()
{

  return $this->handleCreateRequest();
}

And this is the 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 :

if i do a var_dump() of $entity : the result is : string 'GroupeGC\Bundle\ProductBundle\Entity\Product' (length=44) (so it's not null) So why it's doesn't work 0_o

Thomas Trabelsi
  • 330
  • 1
  • 8
  • 21

1 Answers1

1

Did you implement

/**
 * {@inheritdoc}
 */
public function getManager()
{
    return $this->get('your_manager_service_name');
}

In your controller?

And your_manager_service_name should be declared in services.yml something like that:

    your_manager_service_name:
    class: %your_manager_service_name.class%
    arguments:
        - %your.entity.class%
        - @doctrine.orm.entity_manager
ishakuta
  • 136
  • 4