public function getList($wheres = array()) { //查询条件为数据状态大于 -1 的数据 if (!empty($wheres)) { $wheres['status'] = array('gt', -1); } else { $wheres['status'] = array('gt', -1); } //获取分页的工具条 $totalRows = $this->where($wheres)->count(); //准备数数据状态大于-1的总条数 $listRows = 2; //确定每一页显示的数据条数 //实例化分页工具条的对像 $page = new Page($totalRows, $listRows); //>>判断是否要修改主题 if ($totalRows > $listRows) { $page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%'); } //存储分页的条件 $pageHtml = $page->show(); //>>每一页的数据删除完之后显示这页数据的上一页 //查询出每一页需要显示的数据 $this->limit($page->firstRow, $page->listRows)->where($wheres); $rows = parent::select(); return array('rows' => $rows, 'pageHtml' => $pageHtml); }
public function index() { $model = new Model('Category'); $list = $model->select(); $this->assign('list', $list); $this->display(); }
public function db() { $m = new Model('Post'); $list = $m->select(); dump($list); echo $m->getLastSql(); }
function getActionList() { header('Access-Control-Allow-Origin:*'); //跨域 header("Content-type: text/html; charset=utf-8"); $param = json_decode(file_get_contents('php://input'), true); $token = $param['xtoken']; //init_verify_token($token); $t_action = C('T_ACTION'); $model = new Model($t_action); $result = $model->select(); $data["errno"] = 0; $data["action_list"] = $result; echo json_encode($data); }
/** * 获取用户 * @return mixed */ public function getUsers() { $User = new Model('user'); return $User->select(); }
public function testModel() { $config = array('db_type' => 'mysql', 'db_user' => 'root', 'db_pwd' => 'root123', 'db_host' => 'localhost', 'db_port' => '3306', 'db_name' => 'test'); $modle = new Model('user', '', $config); var_dump($modle->select()); }
/** * 使用相套sql语句,代替视图 * */ public function select($options = array()) { if (!empty($this->viewTableName)) { $orgTableName = $options["table"]; $options["table"] = $this->viewTableName; } // $res = parent::select($options); $options["table"] = $orgTableName; return $res; }
public function getCategories() { $Category = new Model('category'); return $Category->select(); }
/** * 重写select方法 以支持associate和sqlOptions * * 如果传入了['hy'=>true]则模型SQL基础配置生效! * 如果调用了associate连贯操作,则会执行框架的连表查询 * * @param array $options 表达式参数 * @return array */ public function select($options = array()) { if (!($arr = $this->isCallHyFunc($options))) { return parent::select($options); } list($hy, $associate) = $arr; if ($associate) { $associate = array_merge($hy ? $this->sqlOptions['associate'] : array(), $this->_associate); } // 收集table if ($associate) { $this->table(array($this->getTableName() => self::HYP)); } // 收集limit if ($hy && !$this->options['limit']) { $this->limit($this->pageOptions['limit']); } // 收集order if ($order = $this->options['order'] ?: ($hy ? $this->pageOptions['order'] : null)) { // 排序字段自动加表别名以避免冲突 if ($associate && is_string($order)) { $order = $this->fieldPre($order, self::HYP); } $this->options['order'] = $order; } // 收集field if ($associate && ($field = $this->options['field'] ?: ($hy ? $this->sqlOptions['field'] : array()))) { // 防止多表中字段冲突 if (is_string($field)) { $field = $this->fieldPre($field, self::HYP); if (is_string($field)) { $field = array($field); } } if (is_array($field)) { foreach ($field as $k => $v) { if (is_numeric($k)) { $field[$k] = $this->fieldPre($field[$k], self::HYP); } } } } else { $field = $this->getAllFields($field, $associate ? self::HYP : ''); } // 收集 where - sqlOptions if ($hy) { // 拷贝副本,不影响下次使用sqlOptions['where'] $this->sqlOptions['_where'] = $this->sqlOptions['where']; // 收集where - search if ($hy && method_exists($this, 'searchMap')) { $this->searchMap(); } if ($this->options['where']['_string']) { $this->addMap($this->options['where']['_string']); unset($this->options['where']['_string']); } // 整合到TP $this->where($this->sqlOptions['_where']); } // where条件中的字段避免冲突 if ($associate && $this->options['where']) { $arr = array(); foreach ($this->options['where'] as $k => $v) { if (0 === strpos($k, '_')) { continue; } $arr[$this->fieldPre($k, self::HYP)] = $v; unset($this->options['where'][$k]); } if ($arr) { $this->where($arr); } } // 连表处理 if (is_array($associate) && $associate) { // 当前表默认别名 HYP常量 $join = array(); foreach ($associate as $v) { $asc = explode('|', $v); // 0=>关联表名(支持别名),1=>当前表外键,2=>关联表主键,3=>要从关联表取出的字段(多个以“,”分隔),4=>限制条件,5=>join类型(默认inner) preg_match('/([\\w\\.\\-\\(\\)]+)\\s*(AS)*\\s*([\\w\\-]*)/i', $asc[0], $m); $asc[0] = $m[3] ?: $m[1]; if ($asc[4]) { $pre = $asc[0] . '.'; $asc[4] = preg_replace_callback('/`([\\w\\-]+)`/', function ($m1) use($pre) { if ($m1[1]) { return $pre . $m1[1]; } }, $asc[4]); } $asc[5] = $asc[5] ? strtoupper($asc[5]) : 'INNER'; $join[] = $asc[5] . ' JOIN `' . DTP . $m[1] . '` AS `' . $asc[0] . '` ON ' . (strpos($asc[1], '.') ? '' : self::HYP . '.') . $asc[1] . ' = ' . $asc[0] . '.' . $asc[2] . ($asc[4] ? ' AND ' . $asc[4] : ''); if (!$asc[3]) { continue; } // 使用 ,, 处理定义字段时可能使用函数而带来的逗号 $asc[3] = str_replace(',,', '#HY#', $asc[3], $c); foreach (explode(',', $asc[3]) as $show) { if ($c) { $show = str_replace('#HY#', ',', $show); } if (preg_match('/([\\w\\.\\-\\(\\)]+)\\s*(AS)*\\s*([\\w\\-]*)/i', $show, $m)) { $m[1] = strpos($m[1], '.') ? $m[1] : $asc[0] . '.' . $m[1]; if ($m[3]) { $field[$m[1]] = $m[3]; } else { $field[] = $m[1]; } } } } $this->join($join); } $this->field($field); // 清理现场 $this->sqlOptions['_where'] = null; $this->_associate = array(); // 全字段尝试解密 if ($options['decrypt']) { $this->sqlOptions['decrypt'] = true; } if ($this->sqlOptions['decrypt']) { $this->afterRead[] = function (&$result) { array_walk($result, 'val_decrypt_quote'); }; } if ($options['scope']) { $this->_scope[$options['scope']] = $this->options; } if ($options['count']) { $this->options['order'] = null; $this->options['limit'] = null; $this->options['field'] = '1'; return count(parent::select()); } if ($options['find']) { $this->options['limit'] = 1; } return parent::select(); }
function addManyDayCourse() { header('Access-Control-Allow-Origin:*'); //跨域 header("Content-type: text/html; charset=utf-8"); $param = json_decode(file_get_contents('php://input'), true); /* $str = '{ "xtoken":"35dsakfsdjfcvjdsajfkdsf234", "coachid":45, "uid":999, "pid":41, "course_list":[ { "course_time":32435435, "action_list":[ { "actionid":177, "group":3, "count":20, "order":1, "type":1, "duration":34 }, { "actionid":188, "group":3, "count":20, "order":1, "type":2, "duration":54 }] }, { "course_time":32438935, "action_list":[ { "actionid":199, "group":3, "count":20, "order":1, "type":1, "duration":34 }, { "actionid":166, "group":3, "count":20, "order":1, "type":2, "duration":54 }] }] }';*/ // $param = json_decode($str,true); $token = $param['xtoken']; init_verify_token($token); $uid = $param['uid']; $pid = $param['pid']; $coachid = $param['coachid']; if (!isset($uid) || !isset($pid) || !isset($coachid)) { err_ret(-205, 'lack of param', '缺少参数'); } $course_list = $param['course_list']; //找到开始时间和结束时间 $begin_time = $course_list[0]['course_time']; $end_time = $course_list[0]['course_time']; for ($i = 0; $i < count($course_list); $i++) { if ($course_list[$i]['course_time'] <= $begin_time) { $begin_time = $course_list[$i]['course_time']; } if ($course_list[$i]['course_time'] >= $end_time) { $end_time = $course_list[$i]['course_time']; } } //课程id $model_course_record = new Model('course_record'); $cur_courseid = $model_course_record->max('courseid') + 1; //获取 pay_time $model_my_plan = new Model('my_plan'); $condition['uid'] = $uid; $condition['pid'] = $pid; $condition['coachid'] = $coachid; $condition['courseid'] = 0; $result_pay_time = $model_my_plan->select(); $pay_time = $result_pay_time[0]['pay_time']; $model_my_plan->where($condition)->delete(); //删除记录为courseid=0的 for ($i = 0; $i < count($course_list); $i++) { //添加课程记录 $action_list = $course_list[$i]['action_list']; for ($j = 0; $j < count($action_list); $j++) { $action_list[$j]['courseid'] = $cur_courseid; $model_course_record->add($action_list[$j]); } //把相应的课程记录添加到my_plan unset($data_my_plan); $data_my_plan['uid'] = $uid; $data_my_plan['pid'] = $pid; $data_my_plan['coachid'] = $coachid; $data_my_plan['courseid'] = $cur_courseid; $data_my_plan['status'] = 3; //课程已经制定 $data_my_plan['iscontacted'] = 0; $data_my_plan['isfinished'] = 0; $data_my_plan['pay_time'] = $pay_time; $data_my_plan['course_time'] = $course_list[$i]['course_time']; $data_my_plan['begin_time'] = $begin_time; $data_my_plan['end_time'] = $end_time; $model_my_plan->add($data_my_plan); $cur_courseid++; } $data['errno'] = 0; $data['uid'] = $uid; $data['pid'] = $pid; $data['coachid'] = $coachid; echo json_encode($data); }
public function getSlides() { $Slide = new Model('slide'); return $Slide->select(); }