static::$baseFile]); } else { return new static::$baseFile; } } // 未设置 $baseFile,配置不完整 throw new InvalidConfigException( 'Missing model binding for ' . static::class . ': set static $baseFile.' ); } /** * 获取模型对象 (ActiveRecord) * @return ActiveRecord * @throws InvalidConfigException */ public static function getActiveRecord() { return self::getModel(); } /** * 查询全部列表 (不分页) * @param string|array $select 查询字段 * @param array $where 查询条件 * @param string|array $order 排序 * @param string|array $with 关联查询 * @return array * @throws \Exception */ public static function getAllList($select, $where, $order = '', $with = '') { $model = self::getModel(); $return = $model->getAllList($select, $where, $order, $with); return $return; } /** * 分页查询列表 (自动获取请求中的page和pageSize) * @param string|array $select 查询字段 * @param array $where 查询条件 * @param string|array $order 排序 * @param string|array $with 关联查询 * @return array ['totalNum' => int, 'totalPage' => int, 'moreData' => int, 'list' => array] * @throws \Exception */ public static function getList($select, $where, $order = '', $with = '') { $get = Yii::$app->request->get(); $page = isset($get['page']) ? $get['page'] : 1; Yii::$app->params['page'] = $page; $pageSize = isset($get['pageSize']) && !empty($get['pageSize']) ? $get['pageSize'] : Yii::$app->params['pageSize']; $model = self::getModel(); $return = $model->getList($select, $where, $page, $pageSize, $order, $with); return $return; } /** * 查询指定条数的列表 * @param string|array $select 查询字段 * @param array $where 查询条件 * @param int $limit 限制条数 * @param string|array $order 排序 * @param string|array $with 关联查询 * @return array * @throws \Exception */ public static function getLimitList($select, $where, $limit = 10, $order = '', $with = '') { $model = self::getModel(); $return = $model->getLimitList($select, $where, $limit, $order, $with); return $return; } /** * 添加数据 * @param array $data 数据数组 * @param bool $returnObject 是否返回对象 * @return array|Base * @throws \Exception */ public static function add($data, $returnObject = false) { $model = self::getModel(); return $model->add($data, $returnObject); } /** * 批量添加 * @param array $data 二维数组 * @return void * @throws \Exception */ public static function batchAdd($data) { $model = self::getModel(); $model->batchAdd($data); } /** * 根据主键ID删除一条记录 * @param int|string $id 主键ID * @return int|false 删除的行数,失败返回false * @throws InvalidConfigException * @throws \Throwable * @throws \yii\db\StaleObjectException */ public static function deleteById($id) { $model = self::getModel(); return $model->deleteById($id); } /** * 根据条件删除一条或多条记录 * @param array $condition 查询条件 * @return int 删除的行数 * @throws \Exception */ public static function deleteByCondition($condition) { $model = self::getModel(); $count = $model->deleteByCondition($condition); return $count; } /** * 根据多个主键ID批量删除 * @param array $ids 主键ID数组 * @return int 删除的行数 * @throws \Exception */ public static function deleteByIds($ids) { $model = self::getModel(); return $model->deleteByIds($ids); } /** * 根据主键ID更新一条记录 * @param int|string $id 主键ID * @param array $data 更新的数据 * @return array ['status' => bool, 'data' => array] * @throws \Exception */ public static function updateById($id, $data) { $model = self::getModel(); return $model->updateById($id, $data); } /** * 根据多个主键ID批量更新 * @param array $ids 主键ID数组 * @param array $data 更新的数据 * @return int 更新的行数 * @throws \Exception */ public static function updateByIds($ids, $data) { $model = self::getModel(); return $model->updateByIds($ids, $data); } /** * 根据条件更新一条或多条记录 * @param array $condition 查询条件 * @param array $data 更新的数据 * @return int 更新的行数 * @throws \Exception */ public static function updateByCondition($condition, $data) { $model = self::getModel(); $count = $model->updateByCondition($condition, $data); return $count; } /** * 根据主键ID查出一条记录 * @param int|string $id 主键ID * @param bool $returnObject 是否返回对象 * @param string|array $field 查询字段 * @return array|ActiveRecord|null * @throws \Exception */ public static function getById($id, $returnObject = false, $field = '*') { $model = self::getModel(); return $model->getById($id, $returnObject, $field); } /** * 获取第一条记录 * @param bool $returnObject 是否返回对象 * @param string|array|bool $order 排序 * @param string|array $field 查询字段 * @return array|ActiveRecord|null * @throws \Exception */ public static function getOne($returnObject = false, $order = false, $field = '*') { $model = self::getModel(); return $model->getOne($returnObject, $order, $field); } /** * 根据条件查出一条记录 * @param array $condition 查询条件 * @param bool $returnObject 是否返回对象 * @param string|array|bool $order 排序 * @param string|array $field 查询字段 * @return array|ActiveRecord|null * @throws \Exception */ public static function getByCondition($condition, $returnObject = false, $order = false, $field = '*') { $model = self::getModel(); return $model->getByCondition($condition, $returnObject, $order, $field); } /** * 判断数据是否存在 * @param array $condition 查询条件 * @return bool * @throws \Exception */ public static function exists($condition) { $model = self::getModel(); return $model->exists($condition); } /** * 根据条件查出多条记录 * @param array $condition 查询条件 * @param string|array|null $order 排序 * @param string|array $field 查询字段 * @param string|callable|null $indexBy 索引字段 * @param bool $returnObject 是否返回对象 * @return array|ActiveRecord[] * @throws \Exception */ public static function getAllByCondition($condition, $order = null, $field = '*', $indexBy = null, $returnObject = false) { $model = self::getModel(); return $model->getAllByCondition($condition, $order, $field, $indexBy, $returnObject); } /** * 根据多个主键ID查询多条记录 * @param array $ids 主键ID数组 * @param string|array|null $order 排序 * @param string|callable|null $indexBy 索引字段 * @param string|array $field 查询字段 * @return array * @throws \Exception */ public static function getByIds($ids, $order = null, $indexBy = null, $field = '*') { $model = self::getModel(); return $model->getByIds($ids, $order, $indexBy, $field); } /** * 根据条件获取记录数量 * @param array $condition 查询条件 * @return int|string * @throws \Exception */ public static function getCount($condition) { $model = self::getModel(); return $model->getCount($condition); } /** * 更新计数器 (原子操作) * @param array $counters 更新的计数器数组 * @param array $condition 更新条件 * @param array $params 绑定参数 * @return int 更新行数 * @throws \Exception */ public static function counters($counters, $condition, $params = []) { $model = self::getModel(); return $model->counters($counters, $condition, $params); } /** * 根据主键获取记录并锁定 (SELECT FOR UPDATE) * @param int|string $id 主键ID * @param string|array $field 查询字段 * @return array|ActiveRecord|null * @throws \Exception */ public static function getLockById($id, $field = '*') { $model = self::getModel(); return $model->getLockById($id, $field); } /** * 求和 * @param array $condition 查询条件 * @param string $field 求和字段 * @return mixed * @throws \Exception */ public static function sum($condition, $field) { $model = self::getModel(); return $model->sum($condition, $field); } }