Error Object of class stdClass could not be converted to string

3

Well I'm with an 'X' object that one of its properties is a JSON. In this JSON it contains the column name and column value respectively.

But I can have several 'X' objects, and so on. Today I'm doing it as follows:

PHP:

 public function SincronizaBanco_MobileWeb(array $params = NULL) {
    $objeto = json_decode($this->params['objJSON']);
    $tamanho = count($objeto);

    try {
        for ($i = 0; $i < $tamanho; $i++) {
            if ($objeto[$i]->FLG_IDENT_OPERA == 'I') {
                $inputs = array();
                $inputsJSON = $objeto[$i]->TXT_COLUN_SINCR;
                $inputs[':values'] = json_decode($inputsJSON);

                print_r($inputs);

                $sincronismo = $this->conexao->save("INSERT INTO " + $objeto[$i]->TXT_TABLE_SINCR + " values (:values);", $inputs);
            } else if ($objeto[$i]->FLG_IDENT_OPERA == 'U') {

            } else {

            }
        }
        print_r("Inserção concluido com sucesso !");
    } catch (Exception $exc) {
        echo $exc->getTraceAsString();
    }
}
}

You are giving the following error:

  

[Sat Dec 05 09:22:57 2015] [error] [client 192.168.1.248] PHP Catchable fatal error: Object of class stdClass could not be converted to string in / opt / lampp / htdocs / renan / connection. php on line 53, referer: link

The error occurs in the following function:

    public function execute(PDOStatement $stmt, array $data = null) {
    try {
        if (isset($data)) {
            $stmt->execute($data);
        } else {
            $stmt->execute();
        }
    } catch (PDOException $exc) {
        echo $exc->getTraceAsString();
        var_dump($exc->getMessage());
    }
}

And what calls this function is

    public function save($sql, array $data) {

    $con = self::getConnection();

    $stmt = $con->prepare($sql);
    print_r($stmt);
    $this->execute($stmt, $data);

    if ($stmt->rowCount()) {
        return true;
    } else {
        return false;
    }
}

How do I resolve this?

My array after json_decode looks like this:

    Array
(
    [0] => stdClass Object
        (
            [COD_IDENT_SINCR] => 1
            [TXT_TABLE_SINCR] => tbl_PESSOAS
            [FLG_IDENT_OPERA] => I
            [TXT_COLUN_SINCR] => {"COD_IDENT_PESSO":"1151205091356692","TXT_NOMEX_PESSO":"iyuiyui","TXT_APELI_PESSO":"yuiyuiy","FLG_SEXOX_PESSO":"","DAT_NASCI_PESSO":"","TXT_NASCI_PESSO":"","TXT_NATUR_PESSO":"","FLG_ESTAD_CIVIL":"S","TXT_FONEX_PESSO":"","FLG_IDENT_PESSO":"A","TXT_EMAIL_PESSO":"","TXT_SENHA_PESSO":"","TXT_ENDER_CEPXX":"","TXT_ENDER_LOGRA":"","TXT_ENDER_BAIRR":"","TXT_ENDER_NUMER":"","TXT_ENDER_COMPL":""}
            [TXT_WHERE_SINCR] => 
            [FLG_IDENT_SINCR] => N
            [COD_IDULT_ATUAL] => -1
            [DAT_ULTIM_ATUAL] => 2015-11-05 09:13:56
        )

    [1] => stdClass Object
        (
            [COD_IDENT_SINCR] => 2
            [TXT_TABLE_SINCR] => tbl_PESSOA_TURMA
            [FLG_IDENT_OPERA] => I
            [TXT_COLUN_SINCR] => {"COD_IDENT_PESSO":"1151205091356692","COD_IDENT_CELUL":"1","FLG_IDENT_PESSO":"M","COD_IDULT_ATUAL":"-1","DAT_ULTIM_ATUAL":"2015-11-05 09:13:56"}
            [TXT_WHERE_SINCR] => 
            [FLG_IDENT_SINCR] => N
            [COD_IDULT_ATUAL] => -1
            [DAT_ULTIM_ATUAL] => 2015-11-05 09:13:56
        )

)
    
asked by anonymous 05.12.2015 / 17:14

1 answer

1
  

Catchable fatal error: Object of class stdClass could not be converted to string in

The error happens when you try to manipulate a compound type (array or object) in a string or try to print any of them with print or echo .

save() receives a string (SQL command) and attempts to execute the operation on the database, but the symbol for concatenating strings is . and not +

$sincronismo = $this->conexao->save("INSERT INTO " + $objeto[$i]->TXT_TABLE_SINCR + " values (:values);", $inputs);

I would rewrite the logic of the method to simplify some things and make others clear.

I would change the for convecional by a foreach to eliminate the $tamanho vairável and to change the $objeto[$i]->algo ins with $item->algo .

It may not have been clear in the question but TXT_COLUN_SINCR seems to be the record to be written to the database though it is like a json so you need to convert it its structure looks like this:

Array
(
    [COD_IDENT_PESSO] => 1151205091356692
    [TXT_NOMEX_PESSO] => iyuiyui
    [TXT_APELI_PESSO] => yuiyuiy
    [FLG_SEXOX_PESSO] => 
    [DAT_NASCI_PESSO] => 
    [TXT_NASCI_PESSO] => 
    [TXT_NATUR_PESSO] => 
    [FLG_ESTAD_CIVIL] => S
    [TXT_FONEX_PESSO] => 
    [FLG_IDENT_PESSO] => A
    [TXT_EMAIL_PESSO] => 
    [TXT_SENHA_PESSO] => 
    [TXT_ENDER_CEPXX] => 
    [TXT_ENDER_LOGRA] => 
    [TXT_ENDER_BAIRR] => 
    [TXT_ENDER_NUMER] => 
    [TXT_ENDER_COMPL] => 
)

The idea is basically to mount the query correctly, first json_decode() on $item->TXT_COLUN_SINCR to return the array above, from it I mount the list of fields and binds with the statement: $campos = implode(',', array_keys($json)); what changes is the list of binds has : before the name fields.

With the list of fields and binds ready, just put them in the string containing SQL, with sprintf() , each %s is replaced by each value in the order, the first places the table name, the second the list of fields and last binds.

public function SincronizaBanco_MobileWeb(array $params = NULL) {
    $objeto = json_decode($this->params['objJSON']);
    try{
        foreach($objeto as $item){
            if($item->FLG_IDENT_OPERA == 'I'){
                $json = json_decode($item->TXT_COLUN_SINCR, true);
                $campos = implode(',', array_keys($json));
                $binds = ':'. implode(',:', array_keys($json));

                $sql = sprintf("INSERT INTO %s (%s) VALUES (%s)", $item->TXT_TABLE_SINCR, $campos, $binds);
                $sincronismo = $this->conexao->save($sql, $json);

            } else if ($objeto[$i]->FLG_IDENT_OPERA == 'U') {
            }else{
            }
        }
    } catch (Exception $exc) {
        echo $exc->getTraceAsString();
    }
}

Another alternative to the second implode() is to use str_replace() to remove : .

$binds = ':'. implode(',:', array_keys($json));
$campos = str_replace(':', '', $binds);
    
06.12.2015 / 01:05