0

假设我有一个返回以下数据的查询:

RangeId | MinValue | MaxValue | Resolution | UnitId | UnitName

我想MeasurementRange用上述数据水合物体。

class MeasurementRange {
    public function getRangeId() {...};
    public function setRangeId($id) {...};
    public function getRange() {...};
    public function setRange(Range $range) {...};
    public function getUnit() {...};
    public function setUnit(Unit $unit) {...};
}

class Range {
    public function getMinValue() {...};
    public function setMinValue(float $minVal) {...};
    public function getMaxValue() {...};
    public function setMaxValue(float $maxVal) {...};
    public function getResolution {...};
    public function setResolution(float $resolution) {...};
}

class Unit {
    public function getUnitId() {...};
    public function setUnitId(int $id) {...};
    public function getUnitName() {...};
    public function setUnitName(string $name) {...};
}

如您所见,该MeasurementRange对象已设置RangeUnit对象。

如何从上述查询中补水MeasurementRange以及内部Range和对象?Unit

PS:我没有指定对象的受保护属性。我想他们是不言而喻的。

4

1 回答 1

1

您需要创建一个映射器,它将使用您的 dbAdapter 以数组的形式获取数据,然后使用水合器来水合所有对象,最后将Rangeand添加UnitMeasurementRange. 您也可以创建一个自定义水合器(就单一责任而言,这会更好)。

我没有时间清理下面的示例,但这就是它的样子:)

final class LanguageMapper
{
    /**
     * @param LanguageTable   $languageTable
     * @param PackageTable    $packageTable
     * @param Cache           $cache
     * @param LoggerInterface $logger
     */
    public function __construct(LanguageTable $languageTable, PackageTable $packageTable, Cache $cache, LoggerInterface $logger)
    {
        $this->languageTable = $languageTable;
        $this->packageTable = $packageTable;
        $this->cache = $cache;
        $this->logger = $logger;
    }

    /**
     * @param array $where
     *
     * @return array List of active languages
     */
    public function findActive(array $where = [])
    {
        try {
            if (empty($where) && $this->cache->hasItem('active_languages')) {
                return unserialize($this->cache->getItem('active_languages'));
            }
        } catch (RuntimeException $exception) {
             $this->logger->critical($exception->getMessage(), [
                'exception' => $exception,
                'file' => $exception->getFile(),
                'line' => $exception->getLine(),
             ]);
        }
        /* @var $adapter \Zend\Db\Adapter\Adapter  */
        $adapter = $this->languageTable->getGateway()->getAdapter();
        $sql = new Sql($adapter);
        $select = $sql->select()->columns([Select::SQL_STAR])
            ->from('language')
            ->join('package', 'package.id = language.package', Select::SQL_STAR, Select::JOIN_LEFT)
            ->where(array_merge($where, ['active' => true]))
            ->order(['position']);
        $selectString = $sql->buildSqlString($select);

        $resultSet = $adapter->query($selectString, Adapter::QUERY_MODE_EXECUTE);
        $languages = [];
        $hydrator = new ArraySerializable();
        foreach ($resultSet as $result) {
            $language = new Language();
            $package = new Package();
            $hydrator->hydrate((array) $result, $package);
            $hydrator->hydrate((array) $result, $language);
            $language->setPackage($package);
            $languages[] = $language;
        }
        if (empty($where)) {
            try {
                $this->cache->setItem('active_languages', serialize($languages));
            } catch (RuntimeException $exception) {
                $this->logger->warning($exception->getMessage(), [
                    'exception' => $exception,
                    'file' => $exception->getFile(),
                    'line' => $exception->getLine(),
                ]);
            }
        }

        return $languages;
    }
}
于 2017-01-25T09:04:15.643 回答