tags:

views:

90

answers:

3

Hello,

I was trying to return a set of objects.

But this code gives me the following error:

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

 public function fetchObject($psClassname ="",$paParams =array()){
          $lrResource = $this->mrQueryResource;
          $liResult = null; 
          while($row = mysql_fetch_object($lrResource,$psClassname,$paParams)){
           $liResult .= $row;     <-this line produces the error
          }
          return $liResult;
         }
A: 

That's because you are trying to convert $row to a string (the .= assumes a string is given on the right hand side)

Andrei Serdeliuc
+4  A: 

In your code $row is a an object (you've used mysql_fetch_object), and the .= operator tries to build a string, concatenating $liResult and $row. I believe this behaviour only works if your object implements a toString method

You could return an array of rows using this code:

public function fetchObject($psClassname ="",$paParams =array()){
     $lrResource = $this->mrQueryResource;
     $liResult = array();
     while($row = mysql_fetch_object($lrResource,$psClassname,$paParams)){
       $liResult[] = $row;
     }
     return $liResult;
}
David Caunt
A: 

Thanks,

How can I itterate through it using:

public function getIterator() { return new ArrayIterator($this->maPersonArray); }

?

sanders
If you've got an array as I suggested you can simply doforeach ($maPersonArray as $row){//do something with $row}as with any other array
David Caunt