Using PDO To Create A Data Access Object

4th August 2020

A Data Access Object (DAO) is a way of taking data out of a database or API and present it in a unified way across your application. As a design pattern this has uses in standardising how a particular bit of data is passed around, without having to resort to using arrays to accomplish the same job.

I was doing some work with PHP's PDO library the other day when I noticed that I could fetch data out of a database using the fetchAll() method with the PDO::FETCH_CLASS flag. This flag automatically returns the data in an object. This got me thinking more about how this worked, but I couldn't find many articles talking about this. Hence this post.

Setting Things Up

Let's take a class called SpecialText that will store an ID and some text, along with a __toString() method to allow the printing of those values. We will use this class throughout this post to store data from the database.

  1. class SpecialText {
  2. protected $id;
  3. protected $specialtext;
  4.  
  5. public function __toString() {
  6. $output = self::CLASS . PHP_EOL;
  7. $output .= 'ID: ' . $this->id . PHP_EOL;
  8. $output .= 'Special Text: ' . $this->specialtext . PHP_EOL;
  9. return $output;
  10. }
  11. }

We will also need a database. The very simplest database to create is with sqlite. The following will create the sqlite database file in the same directory as the PHP script and create a single table called specialtexttable.

  1. $databaseFile = realpath(__DIR__) . '/testdatabase.sqlite';
  2. $databaseHandle = new \PDO('sqlite:' . $databaseFile, '', '', array(\PDO::ATTR_PERSISTENT => false));
  3. $sql = 'DROP TABLE IF EXISTS "specialtexttable";
  4. CREATE TABLE "specialtext" (
  5. "id" INTEGER,
  6. "specialtext" TEXT NOT NULL,
  7. PRIMARY KEY("id" AUTOINCREMENT)
  8. );';
  9. $databaseHandle->exec($sql);

Next, we need to insert some data into the table. A simple insert takes care of that.

  1. $sql = 'INSERT INTO specialtexttable(specialtext) VALUES (:specialtext);';
  2. $query = $databaseHandle->prepare($sql);
  3. $query->execute([':specialtext' => 'text text text']);

Now we are ready to extract data from the database.

Fetch By Indexed Column

By default, the fetchAll() method will return an array containing a combination of indexed and associative items. This means that our table of two values will be wrapped in an array with four values. It is more usual to pass in \PDO::FETCH_ASSOC so that PDO will create an associative array of values from the results. The following example returns an associative array from the database using the fetchAll() method.

  1. $sql = "SELECT * FROM specialtexttable;";
  2. $query = $databaseHandle->prepare($sql);
  3. $query->execute();
  4. $result = $query->fetchAll(\PDO::FETCH_ASSOC);
  5.  
  6. print_r($result);

Note that the fetch() method is essentially equivalent to the fetchAll() method. The difference being that the fetch() method will return a single result, or false if no more results are present and fetchAll() will always return an array of items.

The above code prints out the following.

  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [id] => 1
  6. [specialtext] => text text text
  7. )
  8.  
  9. )

Fetch By stdClass

If you pass the flag \PDO::FETCH_OBJ or \PDO::FETCH_CLASS without any other parameters to the fetchAll() method then you will get back an instance of the stdClass object that maps the column names to the properties in the class.

  1. $sql = "SELECT * FROM specialtexttable;";
  2. $query = $databaseHandle->prepare($sql);
  3. $query->execute();
  4. $objects = $query->fetchAll(\PDO::FETCH_CLASS);
  5.  
  6. print_r($objects);

This will print out the following.

  1. Array
  2. (
  3. [0] => stdClass Object
  4. (
  5. [id] => 1
  6. [specialtext] => text text text
  7. )
  8.  
  9. )

Whilst this is possible, what we are actually getting is little more than an array. It's best not to use the stdClass object unless you really have to as you can't benefit from any of the normal controls or functionality that you would get with a full PHP class. Let's make this more useful by using a class.

Fetch By Class

The \PDO::FETCH_CLASS flag will allow the fetchAll() method to inject values into a class that we specify, calling the constructor afterwards. The second parameter of fetchAll() is the name of the class that you want to use. You may have noticed that the SpecialText class contains protected properties, but this does not matter as PDO will still return an object that maps the column names to your property names. You can even set the class properties as private and PDO would still return a valid object. PDO will essentially bypass the constructor and directly inject properties into your object.

Here is how to use the SpecialText class with fetchAll(). The second parameter must be a string so you could also pass in 'SpecialText', but I have found that using the special ::class constant allows for better code readability when namespacing is involved.

  1. $sql = "SELECT * FROM specialtexttable;";
  2. $query = $databaseHandle->prepare($sql);
  3. $query->execute();
  4. $objects = $query->fetchAll(\PDO::FETCH_CLASS, SpecialText::class);
  5.  
  6. foreach ($objects as $object) {
  7. echo $object;
  8. }

This will print out the following.

  1. SpecialText
  2. ID: 1
  3. Special Text: text text text

That SpecialText object can now be used as any other object.

It's important to remember that the property names of your class must exactly match the column names of your table in order for this to work. If your class property differs from the table name (even by the capitalisation) then PDO will create an object with an additional property that contains that value. For example, if I changed the specialtext property to be specialText then the resulting object would contain a null property called specialText and a property called specialtext that contained the value from the database.

It is possible to override this functionality by adding a __set() method to your class. This magic method is always called if a non-existent property of a class is set. You can then intercept the call and act accordingly.

Constructor Order

If the \PDO::FETCH_CLASS flag is combined with \PDO::FETCH_PROPS_LATE then the constructor is called before the properties are added to the class. This makes the object instantiation behave more like it normally does. In isolation isn't not really clear what changes here, so let's modify the SpecialText class to include a flag for when \PDO::FETCH_PROPS_LATE is used. This is a property that is populated with a string if the specialtext property is null when the constructor is run.

  1. class SpecialText {
  2. protected $id;
  3. protected $specialtext;
  4. protected $propsLate;
  5.  
  6. public function __construct() {
  7. if (is_null($this->specialtext)) {
  8. $this->propsLate = 'late props used';
  9. }
  10. }
  11.  
  12. public function __toString() {
  13. $output = self::CLASS . PHP_EOL;
  14. $output .= 'ID: ' . $this->id . PHP_EOL;
  15. $output .= 'Special Text: ' . $this->specialtext . PHP_EOL;
  16. $output .= 'FETCH_PROPS_LATE: ' . $this->propsLate . PHP_EOL;
  17. return $output;
  18. }
  19. }

We then run the two different variants fetchAll() side by side.

  1. $sql = "SELECT * FROM specialtexttable;";
  2. $query = $databaseHandle->prepare($sql);
  3. $query->execute();
  4. $objects = $query->fetchAll(\PDO::FETCH_CLASS, SpecialText::class);
  5.  
  6. foreach ($objects as $object) {
  7. echo $object;
  8. }
  9.  
  10. $query = $databaseHandle->prepare($sql);
  11. $query->execute();
  12. $objects = $query->fetchAll(\PDO::FETCH_CLASS|\PDO::FETCH_PROPS_LATE, SpecialText::class);
  13.  
  14. foreach ($objects as $object) {
  15. echo $object;
  16. }

Comparing the output of the above it's clear to see that without \PDO::FETCH_PROPS_LATE the propsLate property is not filled in, but with that flag present the property contains the string.

  1. SpecialText
  2. ID: 1
  3. Special Text: text text text
  4. FETCH_PROPS_LATE:
  5.  
  6. SpecialText
  7. ID: 1
  8. Special Text: text text text
  9. FETCH_PROPS_LATE: late props used

Specifying Constructor Arguments

Finally, it is also possible to provide the arguments to the constructor. Let's modify the SpecialText class to include a constructor.

  1. class SpecialText {
  2. private $id;
  3. protected $specialtext;
  4.  
  5. public function __construct($id, $specialtext) {
  6. $this->id = $id;
  7. $this->specialtext = $specialtext;
  8. }
  9.  
  10. public function __toString() {
  11. $output = self::CLASS . PHP_EOL;
  12. $output .= 'ID: ' . $this->id . PHP_EOL;
  13. $output .= 'Special Text: ' . $this->specialtext . PHP_EOL;
  14. return $output;
  15. }
  16. }

Applying the same fetchAll() code as we have used above will present us with the following error.

  1. PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function SpecialText::__construct(), 0 passed and exactly 2 expected in pdo.php:27
  2. Stack trace:
  3. #0 [internal function]: SpecialText->__construct()
  4. #1 pdo.php(106): PDOStatement->fetchAll(1048584, 'SpecialText')
  5. #2 {main}
  6. thrown in pdo.php on line 27

This is because the constructor is still being run and because it is not being passed any parameters the result is a fatal error.

To get around this we can add a third parameter which is the default values that are to be sent to the constructor. The example below sets both of the constructor values to NULL, but as the \PDO::FETCH_PROPS_LATE value is in use these soon get overwritten with the actual values.

  1. $sql = "SELECT * FROM specialtexttable;";
  2. $query = $databaseHandle->prepare($sql);
  3. $query->execute();
  4. $objects = $query->fetchAll(\PDO::FETCH_CLASS|\PDO::FETCH_PROPS_LATE, SpecialText::class, [NULL, NULL]);
  5.  
  6. foreach ($objects as $object) {
  7. echo $object;
  8. }

This prints the same as the output of the two \PDO::FETCH_CLASS calls above.

I have seen people state that the parameters of the array are the field names in the database, this is incorrect as the values of the array are passed directly to the constructor. This is absolutely not an array of field names to map to the constructor.

Set Fetch Mode

Sending the full list of parameters to fetchAll() is always necessary. An alternative approach is to set the fetch mode before running the fetchAll() method.

Here is an equivalent to the previous example, but in this instance we are setting up the fetchAll() operation using the

  1. $sql = "SELECT * FROM specialtexttable;";
  2. $query = $databaseHandle->prepare($sql);
  3. $query->setFetchMode(\PDO::FETCH_CLASS|\PDO::FETCH_PROPS_LATE, SpecialText::class, [null, null]);
  4. $query->execute();
  5. $objects = $query->fetchAll();

Conclusion

Whilst it is possible to use \PDO::FETCH_CLASS in this way, I probably won't be using it on any project work. The work I was talking about at the beginning of the post was just a little hobby project that I was playing around with and needed a quick way of storing data in a database. I've had a look online and can't see this technique being used much in open source projects.

A bit of a problem here is that the property names in the class must be exactly the same as the field names in the database table. This means that your DOA object and its properties are inherently tied into your database. This means you have to be especially careful when naming your fields and properties as you'll have a lot of change to go through if you need to rename things afterwards. You can potentially minimise the impact of tying object properties and field names together by using the magic __set() method.

Running fetch operations this way does cut down a little on writing code, but what you are doing instead is hiding magic property filling and difficult to debug code. Have you ever used this technique? Let me know in the comments.

Add new comment

The content of this field is kept private and will not be shown publicly.