Can not use object of type stdClass as array

0

I am trying to update an object of my bd through my API , I am using Slim Framework 3 but when I execute the postman I get the following error:

  

Can not use object of type stdClass as array

<?php

    use \Psr\Http\Message\ServerRequestInterface as Request;
    use \Psr\Http\Message\ResponseInterface as Response;    

    $app->group('/persona/', function () {

        $app = new \Slim\App;   

        $this->put('editar/{id}', function (Request $request, Response $response) {

            $input = json_decode($request->getBody());
            $array = (array)$input;
            $nombre = $array['nombre']; 
            $apellido = $array['apellido']; 
            $id = $array['id'];

            $sql = "UPDATE persona SET nombre=:nombre, apellido=:apellido, id=:id WHERE id=$id";

            try {   
                $db = new db();
                $db = $db->connect();

                $sth = $db->prepare($sql);
                $sth->bindParam('nombre', $nombre);
                $sth->bindParam('apellido', $apellido);
                $sth->bindParam('id', $id);

                $sth->execute();
                $db = null;

                echo '{"notice": {"text": "Persona Actualizada"}';

            } catch(PDOException $e) {

                echo '{"error": {"text": '.$e->getMessage(). '}';

            }

        });

    });

    
asked by Germanccho 01.03.2018 в 18:29
source

2 answers

1

You are accessing wrongly the data sent from the form, and you are also incorrectly binding the data, according to The Documentation you should do it this way:

 <?php

use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;


$app->group('/persona/', function () {

    $app = new \Slim\App;

    $this->put('editar/{id}', function (Request $request, Response $response) {

        $nombre = $request->getAttribute('nombre');
        $apellido = $request->getAttribute('apellido');
        $id = $request->getAttribute('id');

        $input = json_decode($request->getBody());

        $sql = "UPDATE persona SET nombre=:nombre, apellido=:apellido, id=:id WHERE id=$id";

        $db = new db();
        $db = $db->connect();

        $sth = $db->prepare($sql);
        $sth->bindParam('nombre', $nombre);
        $sth->bindParam('apellido', $apellido);
        $sth->bindParam('id', $id);


        $sth->execute();
        return $this->response->withJson($input);
    });
});
    
answered by 01.03.2018 в 19:25
0

When calling:

$input = json_decode($request->getBody());

The object returned depends on the json that you pass to the function json_decode . If the json is something like the following:

{
    "param1" : 1,
    "param2" : 2,
    "param3" : 3,
    "param4" : 4
}

It will return an object of type stdClass . Therefore, assuming that the data you are trying to access is correct and found in the string of $request->getBody() , you must access them in this way:

$sth->bindParam($nombre, $input->nombre);
$sth->bindParam($apellido, $input->apellido);
$sth->bindParam($id, $input->id);

But if you want to keep your code as you have it, you can pass true as the second parameter of json_decode and then the object you will get will be type array because it will return a array associative.

$input = json_decode($request->getBody(), true);

This way you can access like this:

$sth->bindParam($nombre, $input['nombre']);
$sth->bindParam($apellido, $input['apellido']);
$sth->bindParam($id, $input['id']);

Anyway I do not know where you want to get this data from, if $request->getBody() or the variables that you get with $request->getAttribute , and that will depend on your implementation.

    
answered by 01.03.2018 в 19:33