2013-12-13 12 views
8

Lange Geschichte kurz: Mit FOSRestBundle versuche ich einige Entitäten per POST-Aufruf zu erstellen, oder modifiziere existierend via PUT.FosRestBundle post/put [Erzeuge/aktualisiere Entität] liest nicht korrekt an

hier der Code:

/** 
* Put action 
* @var Request $request 
* @var integer $id Id of the entity 
* @return View|array 
*/ 
public function putCountriesAction(Request $request, $id) 
{ 
    $entity = $this->getEntity($id); 
    $form = $this->createForm(new CountriesType(), $entity, array('method' => 'PUT')); 
    $form->bind($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($entity); 
     $em->flush(); 
     return $this->view(null, Codes::HTTP_NO_CONTENT); 
    } 

    return array(
     'form' => $form, 
    ); 
} //[PUT] /countries/{id} 

Wenn ich rufe/Länder/{id} mit PUT eine json wie { "description": "Japan"} vorbei, es ist mein Land ändern mit id = 1, Putting eine leere Beschreibung.

Wenn stattdessen ich versuche, eine neue Einheit mit dieser Methode zu erstellen:

/** 
* Create new Countries (in batch) 
* @param Request $request json request 
* @return array   redirect to get_coutry, will show the newly created entities 
*/ 
public function postCountriesAction(Request $request) 
{ 
    $entity = new Countries(); 
    $form = $this->createForm(new CountriesType(), $entity); 
    $form->bind($request); 

    if ($form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($entity); 
     $em->flush(); 

     return $this->redirectView(
      $this->generateUrl(
       'get_country', 
       array('id' => $entity->getId()) 
      ), 
      Codes::HTTP_CREATED 
     ); 
    } 

    return array(
     'form' => $form, 
    ); 
} //[PUT {"description":"a_description"}] /countries 

es gibt mir eine Fehlermeldung,:

exception occurred while executing 'INSERT INTO countries (description) VALUES (?)' with params [null]: 
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'description' cannot be null 

so scheint, dass ich nicht in der Lage bin zu passieren richtig die Anfrage an das Formular zu binden.

Beachten Sie, dass, wenn ich die Anfrage json_decode als here es mit einem

{ 
    "code":400, 
    "message":"Validation Failed", 
    "errors":{ 
     "errors":[ 
      "This value is not valid." 
     ], 
     "children":{ 
      "description":[ 
      ] 
     } 
    } 
} 

Jede Beratung

vorgeschlagen antworten?

Danke, Rolls

Antwort

21

I :) gelöst

dies ist der Grund, warum es did'nt gearbeitet:

In meiner Formulardefinition

Der Name war "zanzibar_backendbundle_countries".

public function getName() 
{ 
    return 'zanzibar_backendbundle_countries'; 
} 

So eine Anfrage an dieser Form zu binden, die json so ausgesehen haben sollte:

{"zanzibar_backendbundle_countries": [{"description": "Japan"}]} 

Da ich es so etwas wie

{"id":1,"description":"Italy"} 

ich entfernen musste sein wollte Name aus dem Formular:

public function getName() 
{ 
    return ''; 
} 

In der Regel, wenn Sie eine json mit einem Platzhalter wie

"something":{"key":"value"} 

Formular Name genau „etwas“ muss

0

Try this:

if ($form->isValid()) { 
    $entity = $form->getData();  // <------ you didn't bind entity to the form 
    $em = $this->getDoctrine()->getManager(); 
    $em->persist($entity); 
    $em->flush(); 
+0

leider stellen, dies nicht den Trick tut ... Es scheint, i‘ Ich bin nicht in der Lage, die Anfrage an das Formular zu binden ... was ich [aus debug] habe: 'Anfrage: PUT' ' Anfrage: Array ([Beschreibung] => Japan) ' ' Einheit aus db: Länder Objekt ([id: protected] => 1; [Beschreibung: protected] => Italien) ' ' aus form-> getData() form: Länder Objekt <- ([id: protected] => 1; [description: protected] =>) was es speichert: entity: Countries Object ([id: protected] => 1; [Beschreibung: protected] =>) ' – rollsappletree