/** * 进行单表的,基于等号筛选的查询 * @author 欧远宁 * @param array $filter 筛选条件如:array('uid'=>'outrace')。如果是字符串,则根据ID查找 * @param array $page 分页参数 array('cur'=>当前页数,'size'=>每页数据量, start=开始笔数,不予cur同时使用,<br/> * 'all'=>是否全部取回来y/n,'ttl'=是否返回总数) * @param string $order 排序信息如:'time desc,name asc',默认不排序 * @param array $get_one 所需要获取的一对一表如:array('base.user.user_id','forum.forum_stat') * @example * $dao = new dao('blog','article');<br/> * $filter = array('uid'=>'ouyuanning');<br/> * $page = array('cur'=>1,'size'=>40,'ttl'=>'y'); //取第一页。每页40笔,返回总量<br/> * $order = 'addtime Desc'; //根据时间降序排列<br/><br/> * * //获取1对1表内容,根据原则是article_id = articleStat_id<br/> * //多对1则根据最后一个的字段名的值 = 多对一表的ID值<br/> * $get_one = array('blog.article_stat','user.upoint.user_id');<br/><br/> * * //得到结果为:$res['article_list'] $res['article_stat_list'] $res['upoint_list']<br/> * $res = $dao->get($filter, $page, $order, $get_one); * @return array */ public function get($filter = '', $page = null, $order = null, $get_one = null) { $result[$this->key_list] = array(); if ($filter == '') { $filter = array(); } if (is_array($get_one)) { $one_arr = array(); foreach ($get_one as $obj) { $arr = explode('.', $obj); $one_arr[] = $arr; if (count($arr) == 4) { $result[$arr[3] . '_list'] = array(); } else { $result[$arr[1] . '_list'] = array(); } } } if (is_array($filter)) { //并非根据ID获取 $flen = count($filter); if ($flen == 1 && key_exists($this->key, $filter)) { return $this->get($filter[$this->key], null, null, $get_one); } //有值的普通数组 if ($flen > 0 && !fun::is_assoc($filter)) { foreach ($filter as $fkey) { $tmp = $this->get($fkey, null, null, $get_one); foreach ($result as $k => $v) { $result[$k] = array_merge($v, $tmp[$k]); } } return $result; } $klist = $this->get_rec($filter, $page, $order); if (!is_null($klist['ttl'])) { $result['ttl'] = $klist['ttl']; } foreach ($klist['list'] as $obj) { $tmp = $this->get($obj[$this->key], null, null, $get_one); //一次返回多笔记录的时候,不支持$getMany foreach ($tmp as $k => $v) { if (count($v) > 0) { $result[$k][] = $v[0]; } else { $result[$k][] = array(); } } } } else { //根据ID获取 $id = $filter; if ($id == '') { return array($this->key_list => array()); } $filter_para = array(); $filter_para[':' . $this->key] = $filter; $where = ' WHERE ' . $this->key . ' = :' . $this->key; if ($this->schema['cache'] > -1) { //说明有使用缓存 $cid = $this->ver . $this->mdl . $this->tbl . $id; $re = $this->cache->get($cid); if ($re == '') { //未命中缓存 $sql = 'SELECT ' . $this->schema['fields'] . ' FROM `' . $this->tbl . '`' . $where; $tmp = $this->db->query($sql, $filter_para); if (count($tmp) > 0) { $result[$this->key_list] = $tmp; $this->cache->set($cid, $tmp, $this->schema['cache']); } } else { $result[$this->key_list] = $re; } } else { //未使用缓存 $sql = 'SELECT ' . $this->schema['fields'] . ' FROM `' . $this->tbl . '`' . $where; $result[$this->key_list] = $this->db->query($sql, $filter_para); } $cLen = count($result[$this->key_list]); //得到1对1表数据 if ($cLen > 0 && is_array($get_one)) { foreach ($one_arr as $one) { $c = new dao($one[0], $one[1]); $len = count($one); if ($len < 3) { $tmp = $c->get($id); } else { $tmp = $c->get($result[$this->key_list][0][$one[2]]); } if ($len == 4) { $tmp[$one[3] . '_list'] = $tmp[$one[1] . '_list']; unset($tmp[$one[1] . '_list']); } $result = array_merge($result, $tmp); } } } return $result; }