将 Zend\Db\ResultSet\HydratingResultSet 转换为对象数组 [英] Convert Zend\Db\ResultSet\HydratingResultSet to Array of Objects

查看:27
本文介绍了将 Zend\Db\ResultSet\HydratingResultSet 转换为对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Mapper 类中,我从 ZfcBase 扩展了 AbstractDbMapper 以从数据库中获取行.一个简单的例子是这样的代码:

In my Mapper class I'm extending AbstractDbMapper from ZfcBase to fetch rows from the database. A simple example would be code like this:

class MyMapper extends AbstractDbMapper
{

    //...

    public function fetchAll() {
        $select = $this->getSelect();
        return $this->select($select); // returns HydratingResultSet
    }
}

问题是 $this->select() 返回一个 Zend\Db\ResultSet\HydratingResultSet(包含需要的和水合的对象).但我想返回这些对象的数组,而不是包含这些对象的 HydratingResultSet.

The problem is that $this->select() returns a Zend\Db\ResultSet\HydratingResultSet (containing the needed and hydrated objects). But I would like to return an array of these objects instead of a HydratingResultSet containing the objects.

首先要看的是 HydratingResultSet::toArray() 但这会返回一个多维数组而不是对象数组.

The first thing to look at would be HydratingResultSet::toArray() but this returns a multidimensional array instead of an array of objects.

所以我选择手工制作:

public function fetchAll() {
        $select = $this->getSelect();

        $results = array();
        foreach ($this->select($select) as $object) {
            $results[] = $object;
        }
        return $results; // returns array of needed objects
}

这有效,但在每个 fetch 方法中看起来都很丑陋.我是否必须修改 select() 中的代码才能获得所需的行为,还是有更简单的方法?

This works but looks ugly in every fetch method. Do I have to modify the code from select() to get the wanted behavior or is there an easier way?

顺便说一句:甚至建议返回一个数组或像这样转换它吗?感谢您的帮助!

Btw: Is it even recommended to return an array or convert it like this? Thanks for your help!

推荐答案

更新:

有一种更清洁的可能性(来自 https://stackoverflow.com/a/19266650/1275778 的豪华轿车)一>).适应我上面的例子,它的工作原理是这样的:

There is a cleaner possibility to do it (by limos from https://stackoverflow.com/a/19266650/1275778). Adapted to my example from above it works like this:

public function fetchAll() {
    $select = $this->getSelect();
    $results = $this->select($select);
    return \Zend\Stdlib\ArrayUtils::iteratorToArray($results); // returns desired array of objects
}

如果豪华轿车在这里发布他的答案,我会很乐意接受.

If limos posts his answer here, I will happily accept it.

旧答案:

因为没有人能回答我的问题,所以我尝试实现最干净的选择(对我来说):扩展 AbstractDbMapper 以添加提到的功能.我在这里为任何寻求解决方案的人记录下来:

Since no one could answer my question I tried to implement the cleanest option (to me): extending AbstractDbMapper to add the mentioned functionality. I document it here for anyone looking for a solution:

MyAbstractDbMapper extends AbstractDbMapper
{
    /**
     * @param Select $select
     * @param object|null $entityPrototype
     * @param HydratorInterface|null $hydrator
     * @return array
     */
    protected function select(Select $select, $entityPrototype = null, 
        HydratorInterface $hydrator = null)
    {
        $resultSet = parent::select($select, $entityPrototype, $hydrator);
        $results = array(); // Array of result objects
        foreach ($resultSet as $object) {
            $results[] = $object;
        }
        return $results;
    }
}

MyAbstractDbMapper 中的

select() 现在返回一个对象数组,而不是 HydratingResultSet.

select() in MyAbstractDbMapper now returns an array of objects instead of HydratingResultSet.

由于这被否决了,有人可以解释一下原因吗?

As this is getting downvoted, could someone please explain why?

这篇关于将 Zend\Db\ResultSet\HydratingResultSet 转换为对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆