【问题标题】:access zf2 database result object in controller在控制器中访问 zf2 数据库结果对象
【发布时间】:2013-03-18 15:17:05
【问题描述】:

如何使用 foreach 循环在控制器中循环通过我的数据库结果?

$select = new Select();
$select->from('table_name');
$select->where(array('salt'  => $salt));
$select->where(array('ip'    => $this->_getUserIp()));

$rowset = $this->tableGateway->selectWith($select);

return $rowset;

我想我需要将 db 结果对象转换为数组?

提前致谢

【问题讨论】:

    标签: database arrays controller zend-framework2 resultset


    【解决方案1】:

    http://framework.zend.com/manual/2.1/en/modules/zend.db.result-set.html

    Zend\Db\ResultSet\ResultSet 扩展了traversable,因此您可以在其上使用foreach

    foreach($this->getSomeTable()->fetchAll() as $row) {
        //here you can access the row as an array or use getters if you have set a prototype object
        //eg
        $userId = $row['user_id'];
        $userId = $row->user_id;
        $userId = $row->getId();
    
    }
    

    另外,我建议通读入门指南。所有这些基本的东西都在那里解释。

    http://framework.zend.com/manual/2.1/en/user-guide/overview.html

    【讨论】:

    • 感谢您的回答,我现在从 module.php 中删除了“setArrayObjectPrototype”,这对我有用:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多