Flourish PHP Unframework
This is an archived copy of the forum for reference purposes

how iterate result from buildFromSQL

posted by gigix 8 years ago

Hi, I'm new. I have error ("Fatal error: Cannot use object of type Document as array...") with foreach loop.

Code:

$documents = fRecordSet::buildFromSQL('Document',
"SELECT * FROM documents $limit",
"SELECT count(*) FROM documents"
);
foreach ($documents as $doc){

echo $doc['id'];

}

Help, please!

fActiveRecord uses setter and getter methods to allow for simple modification of behavior on individual classes. So, instead of using $doc['id'] you need to use $doc->getId().

foreach ($documents as $doc){
    echo $doc->getId();
}

If you are dealing with user input, always be sure to use the encode action to prevent cross-site scripting (XSS) vulnerabilities:

foreach ($documents as $doc){
    echo $doc->encodeId();
}
posted by wbond 8 years ago

Thank You!

posted by gigix 8 years ago