020 555 1111
  • Old fashion, modular, procedural PHP plus some scaffolding techniques to allow it to mimic MVC simplicity.
  • A quick reference to help you find in PHP maze what needs to be done and how to do it.
  • Learn a PHP Object Oriented Programming by building a live discussion forum application.
  • MVC - Model View Controller - real life, easy reference and application.
This product has options, take a look at them and select ones you like the most.
Options are displayed under image section.

PDO::FETCH_CLASS
[MySQL PDO Const]

PDO::FETCH_CLASS (integer)

Specifies that the fetch method shall return a new instance of the requested class, mapping the columns to named properties in the class.
Note: The magic __set() method is called if the property doesn't exist in the requested class

Used with:

  • PDO::query  [more]
  • PDOStatement::setFetchMode  [more]
  • PDOStatement:: fetch  [more]
  • PDOStatement:: fetchAll  [more]

 

 

PDO::query

note: you'd be better off using PDPStatement::prepare/execute than this function - especially for repetitive queries

This function takes more than query as an argument. Unfortunately documentations in this regards is scarce, if any at all.
Here is function construction:

PDO::query ( string $statement , int $PDO::FETCH_CLASS , string $classname , array $ctorargs )

Lets take a look at them.

string $statement: e.g. "SELECT entry_title, entry_body, status FROM posts"
int $PDO::FETCH_CLASS: specifies to fetch that statement into selected class / ino other words, to create a new instance od that class.
string $classname: this is a selected class name, that class must exists
array $ctorargs : arguments of custom class constructor

 

code:

class myClass {
    function __construct() {
    }
}

$sql = "SELECT entry_title, entry_body, status FROM posts";
$result = $dbh->query($sql,PDO::FETCH_CLASS,'myClass');
print_r($result);
var_dump($result);

 

output:

print_r:

 
PDOStatement Object
(
    [queryString] => SELECT entry_title, entry_body, status FROM posts
)
 
var dump:
 
object(PDOStatement)[2]
      public 'queryString' => string 'SELECT entry_title, entry_body, status FROM posts' (length=49)
 

 

 

 

PDOStatement::setFetchMode

This function applies default fetch mode for a PDOStatement object.

This can also be done by applying one of the PDO::FETCH_CLASS constant directly inside of PDOStatement::fetch(All).

code:

class ClassName {
    function __construct() {
     //something    
    }
}

$sql = "SELECT entry_title, entry_body, status FROM posts";
$stmt = $dbh->query($sql);
$stmt->setFetchMode(PDO::FETCH_CLASS, 'ClassName');
$result = $stmt->fetch();

print_r($result);
var_dump($result);

 

When fetching properties into an object from query; default: they are fetched before object is constructed. PDO::FETCH_PROPS_LATE cause the properties to be fetched into the object after its been constructed.
Use it like this:

$stmt->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'ClassName');

output:

print_r:

ClassName Object
(
    [entry_title] => post 1
    [entry_body] => 1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    [status] => 0
)

var_dump:

object(ClassName)[3]
     public 'entry_title' => string 'post 1' (length=6)
     public 'entry_body' => string '1: Lorem ipsum dolor sit amet, consectetur adipiscing elit.' (length=324)
     public 'status' => string '0' (length=1)
 

 

 

 

PDOStatement::fetch

You can create class instance using fetch directly.

$sql = "SELECT cn, iso_2, iso_3, fid, sort FROM ct";
$stmt = $dbh->query($sql);
$result = $stmt->fetch( PDO::FETCH_CLASS | PDO::FETCH_CLASSTYPE );

print_r($result);
var_dump($result);

 

output:

 

stdClass Object
(
    [iso_2] => AF
    [iso_3] => AFG
    [fid] => 1
    [sort] => 0
)
 
object(stdClass)[3]
      public 'iso_2' => string 'AF' (length=2)
      public 'iso_3' => string 'AFG' (length=3)
      public 'fid' => string '1' (length=1)
      public 'sort' => string '0' (length=1)

 

Problem:

According to php.net documentation [here] above output should take names from first column as names for created objects. Unfortunately it does not - it returns: stdClass. I will post update, when I find out why.

 

 

PDOStatement::fetchAll

You can create class instance using fetchAll directly. There is same problem with stdClass instead of "first_column_for_class_name". 

class myClass {
    function __construct() {
    }
}

$sql = "SELECT cn, iso_2, iso_3, fid, sort FROM ct";
$stmt = $dbh->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_CLASS, 'myClass', array('__constructor_args'));

print_r($result);
var_dump($result);

 

output:

print_r:

Array
(
    [0] => myClass Object
        (
            [cn] => Afghanistan
            [iso_2] => AF
            [iso_3] => AFG
            [fid] => 1
            [sort] => 0
        )
 
    [1] => myClass Object
        (
            [cn] => Albania
            [iso_2] => AL
            [iso_3] => ALB
            [fid] => 1
            [sort] => 0
        )
)
 
 
var_dump:
 
array
  0 => 
    object(myClass)[3]
      public 'cn' => string 'Afghanistan' (length=11)
      public 'iso_2' => string 'AF' (length=2)
      public 'iso_3' => string 'AFG' (length=3)
      public 'fid' => string '1' (length=1)
      public 'sort' => string '0' (length=1)
  1 => 
    object(myClass)[4]
      public 'cn' => string 'Albania' (length=7)
      public 'iso_2' => string 'AL' (length=2)
      public 'iso_3' => string 'ALB' (length=3)
      public 'fid' => string '1' (length=1)
      public 'sort' => string '0' (length=1)