陷入 Zend_Db_Tables 困境

发布于 2024-11-18 22:57:35 字数 768 浏览 5 评论 0原文

尝试使用 Zend_Db_Tables 任务是从 2 个或可能是 3 个表中检索数据并以 json 形式返回。

代码:

public function getWorkerAction()
{
    $request = $this->getRequest();

    $workers = new table_1();
    if (!$worker) {
        $res = array(
            'success' => false,
            'data' => 'empty',
        );
    }
    else {
        $card = $worker->findParentRow('Table2');
        $res = array(
            'success' => true,
            'data' => array_merge($worker->toArray(), $card->toArray()),
        );
    }

    $this->_helper->json($res);
}

问题是:

  1. 每个字段计数= 30(只需要3-10),
  2. 有些字段是BLOB/CLOB,

为每个地方的每个表生成选择,这对我来说似乎是床解决方案。在这种情况下,我该如何为 findParentRow 生成选择

trying to working with Zend_Db_Tables
task is to retrive data from 2 or may be 3 tables and return as json.

code:

public function getWorkerAction()
{
    $request = $this->getRequest();

    $workers = new table_1();
    if (!$worker) {
        $res = array(
            'success' => false,
            'data' => 'empty',
        );
    }
    else {
        $card = $worker->findParentRow('Table2');
        $res = array(
            'success' => true,
            'data' => array_merge($worker->toArray(), $card->toArray()),
        );
    }

    $this->_helper->json($res);
}

problem is:

  1. field count = 30 in each (need only 3-10)
  2. some fields is BLOB/CLOB

generate select for every table in every place seems bed solution for me. and in this case how shall i generate selects for findParentRow

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

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

发布评论

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

评论(1

葬﹪忆之殇 2024-11-25 22:57:35

听起来您需要一种方法来指定要从父表中选择哪些字段,而不必编写整个 $select 。这将需要一个自定义行类。 ZF 提供了一种简单的方法来做到这一点。在您的依赖表类中,添加如下所示的 rowClass 行:

class Table2 extends Zend_Db_Table_Abstract {
    ...
    protected $_rowClass = 'CustomTableRow';
    ...
}

然后创建如下所示的自定义类,该类将重写 findParentRow 方法以允许您输入简单的字段数组名称:

class CustomTableRow extends Zend_Db_Table_Row {

    public function findParentRow($parentTable, $ruleKey = null, Zend_Db_Table_Select $select = null, array $fields = array()) {
        if ($fields) {
            if ($select) {
                $select->columns($fields);
            } else {
                if (is_string($parentTable)) {
                    $parentTable = $this->_getTableFromString($parentTable);
                } else if (!$parentTable instanceof Zend_Db_Table_Abstract) {
                    throw new Exception("Parent table parameter can only be a string or an instance of Zend_Db_Table_Abstract");
                }

                $select = $parentTable->select()
                    ->from($parentTable, $fields);
            }
        }
        return parent::findParentRow($parentTable, $ruleKey, $select);
    }

}

如果 Zend_Db_Table_Row_Abstract 没有指定第三个输入必须是 Zend_Db_Table_Select 的实例,会更容易,因为这样我们可以自动检查该输入是否是列名数组而不是该类的实例。因此,我们添加自己的第四个输入,并将该逻辑放入方法中。现在您可以在控制器中执行类似的操作:

$worker->findParentRow('Table2', null, null, array('field1', 'field2', ...));

Sounds like you're needing a way to specify which fields you want to select from the parent table without having to write a whole $select. That will require a custom row class. ZF provides an easy way to do this. In your dependent table classes, add a rowClass line like this:

class Table2 extends Zend_Db_Table_Abstract {
    ...
    protected $_rowClass = 'CustomTableRow';
    ...
}

Then make your custom class like this, which overrides the findParentRow method to allow you to input a simple array of field names:

class CustomTableRow extends Zend_Db_Table_Row {

    public function findParentRow($parentTable, $ruleKey = null, Zend_Db_Table_Select $select = null, array $fields = array()) {
        if ($fields) {
            if ($select) {
                $select->columns($fields);
            } else {
                if (is_string($parentTable)) {
                    $parentTable = $this->_getTableFromString($parentTable);
                } else if (!$parentTable instanceof Zend_Db_Table_Abstract) {
                    throw new Exception("Parent table parameter can only be a string or an instance of Zend_Db_Table_Abstract");
                }

                $select = $parentTable->select()
                    ->from($parentTable, $fields);
            }
        }
        return parent::findParentRow($parentTable, $ruleKey, $select);
    }

}

It would be easier if Zend_Db_Table_Row_Abstract didn't specify that the 3rd input has to be an instance of Zend_Db_Table_Select, because then we could automatically check whether that input is an array of column names instead of an instance to that class. So we add a 4th input of our own and put that logic inside the method. Now you can do something like this inside your controllers:

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