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; }
public function setFileds($fileds, $relationEntiyName = null) { if ($relationEntiyName) { $cfg = $this->getConfig(); foreach ($cfg['relations'] as $k => $relation) { if ($relation['name'] == $relationEntiyName) { $crit = null; if (isset($cfg['relations'][$k]['criteria'])) { $crit = $cfg['relations'][$k]['criteria']; } else { if ('my\\bq\\criterion\\MongoCriteriaImpl' == get_class($this->criteria)) { $crit = new MongoCriteriaImpl(new $relation['class']()); } else { $crit = new CriteriaImpl(new $relation['class']()); } } if (!is_array($fileds)) { $fileds = explode(",", $fileds); } $crit->setFileds($fileds); $cfg['relations'][$k]['criteria'] = $crit; break; } } $this->setConfig($cfg); } else { $this->fileds = $fileds; } }
/** * 可以指定条件筛选范本来查询实体; * @param Object $entity 需要查询的实体对象 * @param $example 指定条件样本 * @param array<Order> $orders 指定排序规则; 可以为 Order对像的数组; * @param DataPager $dataPager 数据分页器 * @param array<Translater> $translaters 数据转义器 * @return array<Entity>; */ public function findByExample($entity, $example, $order = null, $dataPager = null, $translaters = null) { if (is_object($entity)) { $config = $entity->getConfig(); $columns = $config['columns']; $criteria = new CriteriaImpl($entity); $criteria->setFileds($entity->getFileds()); //瑕佹煡璇㈢殑瀛楁 $criteria->setExample($example); //set order if (is_object($order)) { $criteria->addOrder($order); } else { if (is_array($order)) { foreach ($order as $orderItem) { $criteria->addOrder($orderItem); } } } if (is_object($translaters)) { $criteria->addTranslater($translaters); } else { if (is_array($translaters)) { foreach ($translaters as $translater) { $criteria->addTranslater($translater); } } } if ($dataPager) { $criteria->setDataPager($dataPager); } return $this->findAll($criteria); } }