I have a PHP class called Product:
JavaScript
x
class Product {
$id;
$name;
}
And another class that get data from database:
JavaScript
$stm = $this->dsn->prepare($sql);
$stm->execute();
$rst = $stm->fetchAll(PDO::FETCH_ASSOC);
How can I convert this PDO resultset ($rst) to an array of objects Product?
Advertisement
Answer
Use the PDO::FETCH_CLASS
argument.
JavaScript
class Product {
public $id;
public $name;
}
$stm = $this->dsn->prepare($sql);
$stm->execute();
$result = $stm->fetchAll( PDO::FETCH_CLASS, "Product" );