0

I have an abstract table class with code similar to this:

function fetchById($id) {
    $id = (int) $id;
    $cacheName = sprintf('%s-%s', stripslashes(get_class($this)), hash('sha256', sprintf(
                    '%s-%s-%s', get_class($this), __FUNCTION__, $id
    )));
    if (($row = $this->cache->getItem($cacheName)) == FALSE) {
        $rowset = $this->tableGateway->select(array('id' => $id));
        $row = $rowset->current();
        if (!$row) {
            throw new \Exception("Could not find row $id");
        }
        $this->cache->setItem($cacheName, $row);
    }

    return $row;
}

This works well enough for the default ArrayObject that $row is returned as, however I am now wanting to include additional functionality into my row objects (so that the functionality is not included in multiple, unrelated, places such as different controllers, etc).

To this end I have created an ArrayObjectPrototype extending Zend\Db\RowGateway\RowGateway, however when I try to cache the row I am getting the following error message: You cannot serialize or unserialize PDO instances

Oh dear.

I have no problem adding __wake and __sleep functions to my row object, but how do I get the PDO object into my __wake function?

I am creating my cache in my application.config.php file:

        'ZendCacheStorageFactory' => function() {
            return \Zend\Cache\StorageFactory::factory(
                array(
                    'adapter' => array(
                        'name' => 'filesystem',
                        'options' => array(
                            'dirLevel' => 2,
                            'cacheDir' => 'data/cache',
                            'dirPermission' => 0755,
                            'filePermission' => 0666,
                        ),
                    ),
                    'plugins' => array('serializer'),
                )
            );
        },

I assume I have to create a custom plugin that I pass the db adaptor into? But I am totally lost on how to do that.

Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129
  • I should look in the direction of Table Gateway as a way of breaking the link between Database layer and Business object layer. – akond Jul 01 '17 at 19:49

1 Answers1

0

What I have done (perhaps incorrectly) is create an abstract row class that does not save the pdo instance. This class extends the RowGateway class but overrides the __construct, initialize, save and delete functions (all the functions that require the pdo

/**
 * Abstract table row class.
 * 
 * @package    RPK
 * @subpackage Db
 * @author     SuttonSilver
 */

namespace RPK\Db;

use Zend\Db\Adapter\Adapter;
use Zend\Db\RowGateway\Exception\RuntimeException;
use Zend\Db\RowGateway\RowGateway;
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\TableIdentifier;
use Zend\Db\RowGateway\Feature\FeatureSet;

abstract class RowAbstract extends RowGateway {

    /**
     * Constructor
     *
     * @param string $primaryKeyColumn
     * @param string|TableIdentifier $table
     * @param Adapter|Sql $adapterOrSql
     * @throws Exception\InvalidArgumentException
     */
    public function __construct($primaryKeyColumn, $table, $adapterOrSql = null) {
        // setup primary key
        $this->primaryKeyColumn = empty($primaryKeyColumn) ? null : (array) $primaryKeyColumn;

        // set table
        $this->table = $table;

        $this->initialize();
    }

    /**
     * initialize()
     */
    public function initialize() {
        if ($this->isInitialized) {
            return;
        }

        if (!$this->featureSet instanceof FeatureSet) {
            $this->featureSet = new FeatureSet;
        }

        $this->featureSet->setRowGateway($this);
        $this->featureSet->apply('preInitialize', []);

        if (!is_string($this->table) && !$this->table instanceof TableIdentifier) {
            throw new RuntimeException('This row object does not have a valid table set.');
        }

        if ($this->primaryKeyColumn === null) {
            throw new RuntimeException('This row object does not have a primary key column set.');
        } elseif (is_string($this->primaryKeyColumn)) {
            $this->primaryKeyColumn = (array) $this->primaryKeyColumn;
        }

        $this->featureSet->apply('postInitialize', []);

        $this->isInitialized = true;
    }

    /**
     * Save any changes to this row to the table
     * @throws \Exception
     */
    public function save() {
        throw new \Exception('No PDO object available.');
    }

    /**
     * Delete this row from the table
     * @throws \Exception
     */
    public function delete() {
        throw new \Exception('No PDO object available.');
    }
}
Richard Parnaby-King
  • 14,703
  • 11
  • 69
  • 129