db->exec() to custom object

39 views
Skip to first unread message

Lou Simard

unread,
Jan 5, 2018, 1:46:10 PM1/5/18
to Fat-Free Framework
Hi,

Is there a way to fetch MySQL object trough db->exec() and mapping it to a custom object like i would do with PDO ie: $stmt->fetch(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'classname') ?
I'm not using Mappers, i write the sql myself...

Thanks,


Lou

xfra35

unread,
Jan 6, 2018, 4:58:20 AM1/6/18
to Fat-Free Framework
It's not possible to directly manipulate the PDO statement, but you could easily create a class or trait for that, something like:

abstract class Mapper {

 
function __construct($row) {
   
foreach ($row as $k=>$v)
     
if (property_exists($this,$k))
        $this
->$k=$v;
 
}

}


class Person extends Mapper {

 
protected $name, $age;

}


foreach ($db->exec($sql) as $row) {
  $person
=new Person($row);
}


Lou Simard

unread,
Jan 8, 2018, 8:23:14 AM1/8/18
to Fat-Free Framework
Thanks for the quick answer xfra35!

Lou
Reply all
Reply to author
Forward
0 new messages