/** * 保存数据,包括更新与插入 * * @param array $attr 需要插入或更新的数据 * @param bool|object $o 如果是更新,需要提供当前更新的对象 * @param string $modelName * * @return array * @throws \Exception */ static function dataSave($attr, $o = false, $modelName = '') { try { if ($o) { // 更新 self::$nowModel = $o; self::$isNew = false; } else { // 插入 self::$isNew = true; if (version_compare(PHP_VERSION, '5.5.0', '>')) { //$staticClass = static::class; //5.5以后的版本可以支持这种写法 $staticClass = self::checkModelName($modelName); if (!$staticClass) { throw new Exception('DataSave Error!'); } } else { if ($modelName) { $staticClass = self::checkModelName($modelName); if (!$staticClass) { throw new Exception('DataSave Error!'); } } else { throw new Exception('DataSave Error!'); } } self::$nowModel = new $staticClass(); } if (self::$nowModel->save($attr) == false) { return self::$nowModel; } } catch (\PDOException $e) { throw new \Exception('Db Failed Error: ' . $e->getMessage()); Di::getDefault()->get('flash')->clear(); Di::getDefault()->get('flash')->error('DB Failed 数据操作发生异常'); return false; } self::dataSaveUpdateStatistics($attr); return true; }
public function initialize() { parent::initialize(); $this->belongsTo('book_id', 'ST\\Common\\Models\\Book', 'id', ['alias' => 'book']); $this->belongsTo('uid', 'ST\\Common\\Models\\User', 'id', ['alias' => 'user']); $this->belongsTo('read_note_id', 'ST\\Common\\Models\\Note', 'id', ['alias' => 'note']); $this->hasMany('id', 'ST\\Common\\Models\\NoteComment', 'p_id', ['alias' => 'note_common', 'foreignKey' => ['action' => Relation::ACTION_CASCADE]]); }
/** * 阅读动态,根据班级、任务类型、阅读状态等筛选出需要的数据 * * @param array $whereArr * @param array $whereMap * @param int $limit * * @return \ST\Common\Models\User * @author Hunter.<*****@*****.**> */ public function getClassesReadBook($whereArr = [], $whereMap = [], $limit = 10) { //todo 使用统计表统计 $builder = $this->di->get('modelsManager')->createBuilder()->columns(['task.id as id', 'book.name as bookName', 'count(DISTINCT page.id) AS readCount', 'count(DISTINCT note.id) AS noteCount', 'avg(log.point) as pointAvg', 'task.start_time', 'task.end_time'])->from(['task' => 'ST\\Common\\Models\\Task'])->leftJoin('ST\\Common\\Models\\Page', 'page.read_task_id=task.id', 'page')->leftJoin('ST\\Common\\Models\\Note', 'note.read_task_id=task.id', 'note')->leftJoin('ST\\Common\\Models\\Book', 'book.id=task.book_id', 'book')->leftJoin('ST\\Common\\Models\\EvaTaskLog', 'log.read_task_id=task.id', 'log')->groupBy('task.id')->orderBy('task.start_time asc'); if ($whereArr) { $i = 0; foreach ($whereArr as $field => $where) { if ($i == 0) { $builder->where($where, $whereMap); } else { $builder->andWhere($where, $whereMap); } $i++; } } $result = BaseModel::getList($builder, $limit, '', '', 'my'); return $result; }
/** * 检测提供的模型名称是否正确 * * @param null $model_name * * @return \ST\Common\Models\BaseModel * @author Hunter.<*****@*****.**> */ protected function checkModelName($model_name = null) { return BaseModel::checkModelName($model_name); }
/** * 学生获取阅读任务 * * @param array $whereArr * @param array $whereMap * @param int $limit * * @return \ST\Common\Models\Task * @author Hunter.<*****@*****.**> */ public function getUserTaskList($whereArr = [], $whereMap = [], $limit = 16) { $builder = $this->di->get('modelsManager')->createBuilder()->columns(['task.*', 'page.last_page', 'page.last_page/book.count_page as _avg', 'book.count_page as book_count_page', 'page.is_finished', 'book.has_question', 'book.image as book_image', 'book.name as book_name', 'book.auth as book_auth', 'book.id as book_id', 'if(note.id,1,0) as note_id'])->from(['task' => 'ST\\Common\\Models\\Task'])->leftJoin('ST\\Common\\Models\\Page', 'page.read_task_id=task.id and page.uid="' . $this->thisController->user->id . '"', 'page')->leftJoin('ST\\Common\\Models\\Book', 'task.book_id=book.id', 'book')->leftJoin('ST\\Common\\Models\\Note', 'note.read_task_id=task.id', 'note')->orderBy('task.start_time asc'); if ($whereArr) { $i = 0; foreach ($whereArr as $field => $where) { if ($i == 0) { $builder->where($where, $whereMap); } else { $builder->andWhere($where, $whereMap); } $i++; } } if ($limit == 1) { return $builder->getQuery()->execute(); } return BaseModel::getList($builder, $limit); }
/** * 根据学校、年级等查询条件获取所有学生 * * @param array $whereArr * @param array $whereMap * @param int $limit * * @return bool|\ST\Common\Models\User * @author Hunter.<*****@*****.**> */ public function getStudentList($whereArr = [], $whereMap = [], $limit = 12) { $page = $this->di->get('dispatcher')->getParam('page', 'int', 1); //组建查询缓存的key $keyStr = __NAMESPACE__ . __METHOD__; if ($whereMap) { $keyStr .= implode(',', $whereArr) . implode(',', $whereMap); } $keyStr .= $page . $limit; $key = md5($keyStr); $result = self::$cache->getCache($key); if ($result === null) { $builder = $this->di->get('modelsManager')->createBuilder()->columns(['u.*', 'classes.name as class_name', 'classes.grade_id as class_grade_id', 'statistics.*', 's40.val as s_read_val', 's50.val as s_note_val', 's60.val as s_avgpoint_val'])->from(['u' => 'ST\\Common\\Models\\User'])->leftJoin('ST\\Common\\Models\\Classes', 'u.classes_id=classes.id', 'classes')->leftJoin('ST\\Common\\Models\\Statistics', 'u.id=statistics.uid and u.classes_id=statistics.classes_id and u.school_id=statistics.school_id', 'statistics')->leftJoin('ST\\Common\\Models\\Statistics', 's40.uid=statistics.uid and s40.ab_id=40', 's40')->leftJoin('ST\\Common\\Models\\Statistics', 's50.uid=statistics.uid and s50.ab_id=50', 's50')->leftJoin('ST\\Common\\Models\\Statistics', 's60.uid=statistics.uid and s60.ab_id=60', 's60')->groupBy('u.id')->orderBy('s60.val+0 desc'); if ($whereArr) { $i = 0; foreach ($whereArr as $field => $where) { if ($i == 0) { $builder->where($where, $whereMap); } else { $builder->andWhere($where, $whereMap); } $i++; } } $result = BaseModel::getList($builder, $limit, '', '', 'my'); self::$cache->setCache($key, $result); } return $result; }