php - PDO FETCH_CLASS when fields and columns have different names -


from documentation fetchall , fetch_class :

<?php class fruit {     public $name;     public $colour; }  $sth = $dbh->prepare("select name, colour fruit"); $sth->execute();  $result = $sth->fetchall(pdo::fetch_class, "fruit"); var_dump($result); ?> 

it happens columns names different fields defined in classes. i'm wondering if there clean way fields of class populated when query fetches different columns names :

$sth = $dbh->prepare("select f_name, f_colour fruit"); 

is there generic solution map columns name fiels ? tried several combinations fetch_mode, hoping 1 of them pass results constructor, none of them worked.

it happens columns names different fields defined in classes.

it shouldn't happen in first place. purpose of fetch mode make properties assignment simple, when column names considered match class properties. so, programmers make use same naming way through - sql table column names, html form input fields names, corresponding classes properties. , true generic solution.

but well, if want it, there 2 solutions, sql based, using aliases in query, , oop based, can employ magic __set() method in can implement whatever rename logic wish.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -