Zend Framework 中使用 Zend_Db_Table 的 CakePHP 风格数据库查询结果?

发布于 2024-12-26 06:46:53 字数 1064 浏览 3 评论 0原文

如果您有一个涉及许多连接的复杂 SQL 查询(例如返回具有关联的多对多标签的文章),Zend Framework 中是否有任何内容可以生成可爱的 CakePHP 风格的数据库结果:

Array
(
    [0] => Array
        (
            [ModelName] => Array
                (
                    [id] => 83
                    [field1] => value1
                    [field2] => value2
                    [field3] => value3
                )

            [AssociatedModelName] => Array
                (
                    [id] => 1
                    [field1] => value1
                    [field2] => value2
                    [field3] => value3
                )

        )
)

我不介意它是一个对象而不是一个对象。一个数组,我只是想知道通过使用 Zend_Db_Table 构建 SELECT JOIN 查询是否可以节省一些跑腿工作并获得一些格式良好的结果。

这是我用来构建查询的代码:

$select = $db->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
$select->from('tableName','fieldName')
     ->join('joinTable', 'joinTable.keyId = tableName.keyId',array())
     ->where('tableName.userId = ?', $userId);
$resultSet = $db->fetchAll($select);

If you have a complex SQL query involving many joins (for example returning Articles with their associated many to many Tags) is there anything in Zend Framework that will produce the lovely CakePHP style database results:

Array
(
    [0] => Array
        (
            [ModelName] => Array
                (
                    [id] => 83
                    [field1] => value1
                    [field2] => value2
                    [field3] => value3
                )

            [AssociatedModelName] => Array
                (
                    [id] => 1
                    [field1] => value1
                    [field2] => value2
                    [field3] => value3
                )

        )
)

I don't mind if it's an object rather than an array, I just wondered if by using Zend_Db_Table to build a SELECT JOIN query I could save some leg work and get some nicely formatted results.

Here is the kind of code I'm using to build the query:

$select = $db->select(Zend_Db_Table::SELECT_WITH_FROM_PART);
$select->from('tableName','fieldName')
     ->join('joinTable', 'joinTable.keyId = tableName.keyId',array())
     ->where('tableName.userId = ?', $userId);
$resultSet = $db->fetchAll($select);

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

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

发布评论

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

评论(1

暗恋未遂 2025-01-02 06:46:54

没有什么比您所习惯的我所要求的数据更漂亮的了。
正常的 Result 将是一个行集对象,但是 ->toArray() 可用于大多数 *Zend_DbTable_Abstract* 方法。

$result->toArray() 使用 Zend_debug::dump() 截断并转储:

Lead Tracks array(7) {
  [0] => array(9) {
    ["trackid"] => string(2) "24"
    ["weekendid"] => string(1) "8"
    ["shiftid"] => string(1) "1"
    ["bidlocationid"] => string(1) "1"
    ["qty"] => string(1) "2"
    ["lead"] => string(1) "1"
    ["bidloc"] => string(14) "out of service"
    ["deptcode"] => string(3) "491"
    ["stationid"] => string(1) "1"
}

查询:

where = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
                     ->setIntegrityCheck(FALSE);
    $where->where('track.bidlocationid = ?', $bidlocationId)
          ->where('lead = ?', $lead)
          ->join('bidlocation', 'bidlocation.bidlocationid = track.bidlocationid')
          ->where('bidlocation.stationid = ?', $stationId)
          ->order('shiftid ASC')
          ->order('weekendid ASC');

    $result = $this->fetchAll($where);

抱歉,只是实用程序:)

Nothing as pretty as what you're used to just the data I asked for.
The normal Result would be a rowset object, however the ->toArray() is available for most *Zend_DbTable_Abstract* methods.

The $result->toArray() truncated and dumped using Zend_debug::dump():

Lead Tracks array(7) {
  [0] => array(9) {
    ["trackid"] => string(2) "24"
    ["weekendid"] => string(1) "8"
    ["shiftid"] => string(1) "1"
    ["bidlocationid"] => string(1) "1"
    ["qty"] => string(1) "2"
    ["lead"] => string(1) "1"
    ["bidloc"] => string(14) "out of service"
    ["deptcode"] => string(3) "491"
    ["stationid"] => string(1) "1"
}

The query:

where = $this->select(Zend_Db_Table::SELECT_WITH_FROM_PART)
                     ->setIntegrityCheck(FALSE);
    $where->where('track.bidlocationid = ?', $bidlocationId)
          ->where('lead = ?', $lead)
          ->join('bidlocation', 'bidlocation.bidlocationid = track.bidlocationid')
          ->where('bidlocation.stationid = ?', $stationId)
          ->order('shiftid ASC')
          ->order('weekendid ASC');

    $result = $this->fetchAll($where);

sorry, just utility :)

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