Convert class object to String

Asked

Viewed 1,698 times

0

I’m trying to display the database query, but when trying to display the error appears Object of class TDataTable could not be converted to string. and pointing the error to the file frm_noticia.php... What must I do to fix this mistake ?

Obs:. Ordem de Processo (controller_noticia > view_noticia > frm_noticia)

controller_noticia.php

function obterIdLink() {
        $sql = "SELECT MAX(noticia_id) FROM noticia";
        $result = $this->getConexao()->executeQuery($sql);

        if($result != null) {
            return $result;
        } else {
            return null;
        }
    }

view_noticia.php

function obterIdLink() {
        return $this->getController()->obterIdLink();
}

frm_noticia.php

echo $view->obterIdLink();

Tdatatable.php

class TDataTable {
    private $FRows  = array();

    function __construct ( $resource ) {
        switch(DB_TYPE){
            case "MYSQL":
                while ($row = mysql_fetch_assoc($resource)) {
                    $this->FRows[] = new TDataRow($row);
                }
                break;
        }
    }

    function Rows() {
        return $this->FRows;
    }

    function RowCount() {
        return count($this->FRows);
    }

    function bind($object, $line) {
        throw new Exception("Nao implementado");
    }

    function getRow($pos) {
        return $this->FRows[$pos];
    }
}
  • 2

    You are trying to echo a Resource which is the result of the query in the database. Hence the error.

  • @Diegof I’m a beginner in PHP, I don’t understand what you mean... if it’s not too much trouble you could explain it to me better ?

  • Enter the code of executeQuery()

  • @rray if you observe you will see that already exists in Arq. controller_noticia.php

  • Does this framework have a manual? it seems to have a very strict flow

  • @rray Actually it does not have that is a part of the company design code where I work, and like the software is all disorganized

Show 1 more comment

1 answer

1


In the frm_noticia.php you’re giving echo on result bank. Should pass mysql_fetch_assoc to take the die result.

To better understand a var_dump($view->obterIdLink())

  • And the worst I’ve ever done that ! kkkk object(TDataTable)#6 (1) { ["FRows":"TDataTable":private]=> array(1) { [0]=> object(TDataRow)#3 (1) { ["MAX(noticia_id)"]=> string(5) "15434" } } }

  • 1

    The result is in Frows Your checkIdLink from view_noticia.php should return only MAX(noticia_id) and not Tdatatable

  • But kind of like how I do to display that result ?

  • 1

    You can use var_dump(get_class_methods ($object)); to find out which methods are available One cool tip is to use kintphp as a very good tool to dump obejtos http://raveren.github.io/kint/

  • array(5) { [0]=> string(11) "__construct" [1]=> string(4) "Rows" [2]=> string(8) "RowCount" [3]=> string(4) "bind" [4]=> string(6) "getRow" }

  • 1

    Exactly, there is getRow. Now if you walk the path you arrive at the result. $arrayResult = $view->obtainIdLink()->getRow(0) ; $max = $arrayResult['"MAX(noticia_id)']; I think this is XD

  • sorry to bother you again but, this Cód. goes in view, controller or frm_noticia ?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.