Lemon\RestBundle\Controller\ResourceController::putAction PHP Method

putAction() public method

public putAction ( Request $request, string $resource, integer $id ) : Response
$request Symfony\Component\HttpFoundation\Request
$resource string
$id integer
return Symfony\Component\HttpFoundation\Response
    public function putAction(Request $request, $resource, $id)
    {
        return $this->handler->handle($request, $this->response, $resource, function (ManagerInterface $manager, $object) use($id) {
            $reflection = new \ReflectionObject($object);
            $property = $reflection->getProperty('id');
            $property->setAccessible(true);
            $property->setValue($object, $id);
            $manager->update($object);
            return $object;
        });
    }

Usage Example

 public function testPutWithIdsForOneToManyRelationships()
 {
     $mustang = new Car();
     $mustang->name = 'Mustang';
     $mustang->year = '2014';
     $this->em->persist($mustang);
     $this->em->flush();
     $person = new Person();
     $person->name = "Stan Lemon";
     $this->em->persist($person);
     $this->em->flush();
     $this->em->clear();
     $request = $this->makeRequest('PUT', '/person/' . $person->id, json_encode(array('name' => 'Stan Lemon', 'cars' => array($mustang->id))));
     $response = $this->controller->putAction($request, 'person', $person->id);
     $this->assertTrue($response->isSuccessful());
     $person = $this->em->getRepository('Lemon\\RestBundle\\Tests\\Fixtures\\Person')->findOneBy(array('id' => $person->id));
     $this->assertNotNull($person);
     $this->assertEquals(array('Mustang'), array_map(function ($car) {
         return $car->name;
     }, $person->cars->toArray()));
 }