【问题标题】:How to use \Zend\Cache andZend\Db\RowGateway\RowGateway together?如何同时使用\Zend\Cache 和Zend\Db\RowGateway\RowGateway?
【发布时间】:2017-06-30 15:59:33
【问题描述】:

我有一个抽象表类,其代码类似于:

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;
}

这对于默认的ArrayObject 来说已经足够好了,$row 被返回为,但是我现在想在我的行对象中包含其他功能(以便该功能不包含在多个不相关的地方,例如不同的控制器等)。

为此,我创建了一个 ArrayObjectPrototype 扩展 Zend\Db\RowGateway\RowGateway,但是当我尝试缓存该行时,我收到以下错误消息:You cannot serialize or unserialize PDO instances

亲爱的。

__wake__sleep 函数添加到我的行对象中没有问题,但是如何将PDO 对象添加到我的__wake 函数中?

我正在我的 application.config.php 文件中创建缓存:

        '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'),
                )
            );
        },

我假设我必须创建一个自定义插件,然后将数据库适配器传递给它?但我完全不知道该怎么做。

【问题讨论】:

  • 我应该把表网关的方向看作是打破数据库层和业务对象层之间联系的一种方式。

标签: zend-framework2 zend-db zend-cache


【解决方案1】:

我所做的(可能是错误的)是创建一个不保存 pdo 实例的抽象行类。该类扩展了RowGateway 类,但覆盖了__constructinitializesavedelete 函数(所有需要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.');
    }
}

【讨论】:

    猜你喜欢
    • 2016-10-07
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多