public function fetchRelationData($tableEntity, $config) { $entityCfg = $tableEntity->getConfig(); $key = $tableEntity->{$config}['key']; $config['class'] = new $config['class'](); $fileds = $config['class']->getFileds(); $relateCfg = $config['class']->getConfig(); if ($fileds[0] == '*') { $fileds = $relateCfg['columns']; } $crit = null; if (isset($config['criteria']) && is_object($config['criteria'])) { $crit = $config['criteria']; } else { $crit = new CriteriaImpl($config['class']); $crit->setFileds($fileds); } $crit->add(Criteria::_AND, Restrictions::eq($config['column'], $key)); $cache = CacheManager::getInstance(); $totalNum = 0; $fileds = $crit->getFileds(); $projectionEntitys = $crit->getProjectionEntitys(); $dataPager = $crit->getDataPager(); //获取分页器 if (isset($projectionEntitys) && count($projectionEntitys) > 0) { $crit->cleanFileds(); $crit->cleanLimit(); $sql = $crit->sql(); $parmas = $crit->getParameters(); $rs1 = $cache->get(CriteriaQuery::getDumpSQL($sql, $parmas)); if (!$rs1) { $rs1 = $crit->_array(false); $cache->set(CriteriaQuery::getDumpSQL($sql, $parmas), $rs1); } $totalNum = @current(@current($rs1)); if (is_object($dataPager)) { $dataPager->setTotalNum($totalNum); } $crit->cleanProjection(); } $crit->setFileds($fileds); if (is_object($dataPager)) { $crit->setFirstResult($dataPager->getFirstResult()); $crit->setFetchSize($dataPager->getPageSize()); } $sql = $crit->sql(); $parmas = $crit->getParameters(); $data = $cache->get(CriteriaQuery::getDumpSQL($sql, $parmas)); if ($data) { if ($data === true) { return null; } return $data; } $rs = $crit->_array(false); if (!$rs) { //如果为空表示数据库没有数据,但缓存还是需要写; $cache->set(CriteriaQuery::getDumpSQL($sql, $parmas), true); } if ($rs) { $class = get_class($config['class']); if ($config['relation'] == 'one-to-one') { $rs = current($rs); $data = new $class(); $data->setFileds($fileds); foreach ($rs as $k => $v) { $k = substr($k, strpos($k, "___") + 3); $data->{$k} = $v; } //检查多级关联 $cfgl = $data->getRelationPro(); if (isset($cfgl) && is_array($cfgl)) { foreach ($cfgl as $pro => $cfg) { if (!isset($cfg['lazy']) || !$cfg['lazy'] == true) { $data->{$cfg}['name'] = $this->fetchRelationData($data, $cfg); } } } $data->setCriteria($this); $cache->set(CriteriaQuery::getDumpSQL($sql, $parmas), $data); $data->setIsNewRecord(false); return $data; } else { $dataList = array(); foreach ($rs as $i => $item) { $data = new $class(); $data->setFileds($fileds); foreach ($item as $k => $v) { $k = substr($k, strpos($k, "___") + 3); $data->{$k} = $v; } $data->setCriteria($this); //检查多级关联 $cfgl = $data->getRelationPro(); if (isset($cfgl) && is_array($cfgl)) { foreach ($cfgl as $pro => $cfg) { if (!isset($cfg['lazy']) || !$cfg['lazy'] == true) { $data->{$cfg}['name'] = $this->fetchRelationData($data, $cfg); } } } $data->setIsNewRecord(false); array_push($dataList, $data); } return $dataList; } } //} return null; }