【问题标题】:Zend_Db custom query to db rows instead of arrayZend_Db 自定义查询到 db 行而不是数组
【发布时间】:2012-06-09 02:26:53
【问题描述】:

当我在我的 DbTable 上调用 fetchAll() 时,我会在 DbTable 中定义的正确 DbRow 类中得到结果。

但是当我像这样创建自定义查询时,我会在数组中得到结果。是否有任何参数可以强制在 DbRows 中接收此数据,或者我应该自己创建行并用这些数组填充它们?

$query = $this->_dbTable->getDefaultAdapter()->select()
        ->from('doctor.doctor')
        ->joinInner('facility.doctorfacility', 'facility.doctorfacility.doctor_id = doctor.doctor.id')
        ->joinInner('facility.facility', 'facility.doctorfacility.facility_id = facility.facility.id')
        ->where(implode(' AND ', $conds));

return $this->_dbTable->getDefaultAdapter()->fetchAll($query);

【问题讨论】:

    标签: php zend-framework zend-db zend-db-table


    【解决方案1】:

    “但是当我像这样创建自定义查询时,我会在数组中得到结果”

    你得到一个数组是因为你正在调用Zend_Db_Adapter_Abstract::fetchAll(),根据代码中的文档块返回一个数组:-

    /**
     * Fetches all SQL result rows as a sequential array.
     * Uses the current fetchMode for the adapter.
     *
     * @param string|Zend_Db_Select $sql  An SQL SELECT statement.
     * @param mixed                 $bind Data to bind into SELECT placeholders.
     * @param mixed                 $fetchMode Override current fetch mode.
     * @return array
     */
    public function fetchAll($sql, $bind = array(), $fetchMode = null)
    

    “当我在我的 DbTable 上调用 fetchAll() 时,我会在 DbTable 中定义的正确 DbRow 类中得到结果。”

    当你这样做时,你正在调用Zend_Db_Table_Abstract::fetchAll(),根据代码中的文档块返回一个Zend_Db_Table_Rowset:-

    /**
     * Fetches all rows.
     *
     * Honors the Zend_Db_Adapter fetch mode.
     *
     * @param string|array|Zend_Db_Table_Select $where  OPTIONAL An SQL WHERE clause or Zend_Db_Table_Select object.
     * @param string|array                      $order  OPTIONAL An SQL ORDER clause.
     * @param int                               $count  OPTIONAL An SQL LIMIT count.
     * @param int                               $offset OPTIONAL An SQL LIMIT offset.
     * @return Zend_Db_Table_Rowset_Abstract The row results per the Zend_Db_Adapter fetch mode.
     */
    public function fetchAll($where = null, $order = null, $count = null, $offset = null)
    

    “是否有任何参数可以强制在 DbRows 中接收此数据,或者我应该自己创建行并用这些数组填充它们?”

    没有,但如果您在正确的对象上调用正确的方法,您将返回您的行集。

    为此改变这一行:-

    $query = $this->_dbTable->getDefaultAdapter()->select()
    

    收件人:-

    $query = $this->_dbTable->select()
    

    这一行:-

    return $this->_dbTable->getDefaultAdapter()->fetchAll($query);
    

    收件人:-

    return $this->_dbTable->fetchAll($query);
    

    这应该可以满足您的需求。如果您卡在 ZF 中,查看代码总是值得的,它是迄今为止最好的文档。

    【讨论】:

    • 更改后,我得到了 DbRows 的数组行集。我还必须将完整性检查设置为 false。那么最终在 zend 中要求的可能是什么?
    【解决方案2】:

    您不能强制它,因为数据将来自多个表,因此 zend_db 无法进行映射。它留给我们将数据映射到实体。

    如果你想要对象数组,那么可以改变获取模式。

    Zend_Db::FETCH_OBJ:以对象数组的形式返回数据。默认 class 是 PHP 内置类 stdClass。结果集的列 可作为对象的公共属性使用。

    仅供参考:Fetch Mode

    像 Doctrine 这样的框架允许我们提供映射器,我们可以在其中传递自定义映射器对象。

    【讨论】:

    • 但我只需要医生表的数据。连接用于过滤行,但我想如果我从第二个参数中只精确医生表列,它不会改变?
    猜你喜欢
    • 2019-06-13
    • 2019-06-26
    • 1970-01-01
    • 2018-08-25
    • 2012-09-05
    • 1970-01-01
    • 2019-10-31
    • 2017-06-09
    • 2022-11-20
    相关资源
    最近更新 更多