Zend_Db_Table_Row_Abstract 扩展行类不起作用

发布于 2024-12-28 18:20:11 字数 1766 浏览 2 评论 0原文

我正在使用 Zend_Db 框架,但遇到了障碍。我试图在行级别向列命名添加自定义处理,但由于某种原因无法调用我的函数。

我已经简化了这个问题,只是尝试找出底层的“Row”类是否被创建过。据我所知,事实并非如此。

这是我得到的:

// this class functions correctly; I get "table" written to my output
class DHR_Table extends Zend_Db_Table_Abstract
{
    protected $_rowClass = 'DHR_Row';

    function __construct(){
        echo "table";
        parent::__construct();
    }
}

// this class never gets called, at least not that is evident from the constructor echo
class DHR_Row extends Zend_Db_Table_Row_Abstract
{
    protected $inflector = null;

    function __construct(){
        echo "row";
        parent::__construct();
    }
}

// this is the actual implementation class that uses these two:
class Application_Model_DbTable_Applicants extends DHR_Table
{
    protected $_name = 'applicants';
}

我的输出包括一些数据(本文中排除,但属于“申请人”类的一部分)和“表”,但没有“行”。任何想法为什么会发生这种情况? Zend 框架版本 1.11.11。

[编辑] 这是用法:

class ApplicantsController extends DHR_RestController
{    
    public function indexAction()
    {
        $applicants = new Application_Model_DbTable_Applicants();
        $result = $applicants->fetchAll();
        $this->success($result);
    }
    protected function success($data, $code = 200)
    {
            if(is_a($data, 'Zend_Db_Table_Rowset')){
                    // we could do some pagination work here
                    $data = $data->toArray();
            }
            $this->setResponseCode($code)->appendBody(Zend_Json::encode(array(
                    'success'=>true,
                    'data' => $data
            )));
    }
}

我希望在返回序列化结果时至少在行类上调用一些方法...

[更新] 如果我使用“fetchRow”,一切都会按预期工作; fetchAll 根本不进行到底层对象类型的转换。

I'm using the Zend_Db framework, and I've run into a snag. I'm trying to add custom handling to the column naming at the row level, but it's failing to invoke my function for some reason.

I've stripped down the problem to simply try and figure out if the underlying "Row" class is ever even created. From what I can tell, it isn't.

Here's what I've got:

// this class functions correctly; I get "table" written to my output
class DHR_Table extends Zend_Db_Table_Abstract
{
    protected $_rowClass = 'DHR_Row';

    function __construct(){
        echo "table";
        parent::__construct();
    }
}

// this class never gets called, at least not that is evident from the constructor echo
class DHR_Row extends Zend_Db_Table_Row_Abstract
{
    protected $inflector = null;

    function __construct(){
        echo "row";
        parent::__construct();
    }
}

// this is the actual implementation class that uses these two:
class Application_Model_DbTable_Applicants extends DHR_Table
{
    protected $_name = 'applicants';
}

My output includes some data (excluded from this post, but part of the "Applicants" class) and "table", but no "row". Any ideas why this might be happening? Version 1.11.11 of the Zend framework.

[Edit]
Here's the usage:

class ApplicantsController extends DHR_RestController
{    
    public function indexAction()
    {
        $applicants = new Application_Model_DbTable_Applicants();
        $result = $applicants->fetchAll();
        $this->success($result);
    }
    protected function success($data, $code = 200)
    {
            if(is_a($data, 'Zend_Db_Table_Rowset')){
                    // we could do some pagination work here
                    $data = $data->toArray();
            }
            $this->setResponseCode($code)->appendBody(Zend_Json::encode(array(
                    'success'=>true,
                    'data' => $data
            )));
    }
}

I would expect to at least have some method on the row class invoked when returning the serialized results...

[Update]
If I use "fetchRow" everything works as expected; fetchAll simply does't do the conversion to the underlying object type.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁的新欢旧爱 2025-01-04 18:20:11

我只是在查看 row/abstract 类的代码。
尝试为 $_tableClass 设置一个值。
$_tableClass = 'DHR_Table';

恐怕这不起作用,因为它看起来像 Zend/Db/Table/Row/Abstract无论如何,.php 都会查找表定义,因此如果不进一步扩展,您似乎追求的抽象级别可能无法实现。

//excerpt from __construct Zend/Db/Table/Row/Abstract.php

public function __construct(array $config = array())
    {
        if (isset($config['table']) && $config['table'] instanceof Zend_Db_Table_Abstract) {
            $this->_table = $config['table'];
            $this->_tableClass = get_class($this->_table);
        } elseif ($this->_tableClass !== null) {
            $this->_table = $this->_getTableFromString($this->_tableClass);
        }
 // cont...
 // Retrieve primary keys from table schema
        if (($table = $this->_getTable())) {
            $info = $table->info();
            $this->_primary = (array) $info['primary'];
        }

        $this->init();

I was just looking at the code for the row/abstract class.
Try setting a value for $_tableClass.
$_tableClass = 'DHR_Table';

I'm afraid that won't work as it looks like Zend/Db/Table/Row/Abstract.php is going to look for a table definition no matter what, so the level of abstraction you seem to be after may not be possible without further extending.

//excerpt from __construct Zend/Db/Table/Row/Abstract.php

public function __construct(array $config = array())
    {
        if (isset($config['table']) && $config['table'] instanceof Zend_Db_Table_Abstract) {
            $this->_table = $config['table'];
            $this->_tableClass = get_class($this->_table);
        } elseif ($this->_tableClass !== null) {
            $this->_table = $this->_getTableFromString($this->_tableClass);
        }
 // cont...
 // Retrieve primary keys from table schema
        if (($table = $this->_getTable())) {
            $info = $table->info();
            $this->_primary = (array) $info['primary'];
        }

        $this->init();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文