| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- <?php
- namespace common\base\classes;
- use Yii;
- use \common\base\models\Base;
- use yii\base\InvalidConfigException;
- use yii\db\ActiveRecord;
- class BaseClass
- {
- public static $baseFile; // 模型类名的路径字符串
- /**
- * 获取模型对象
- * @return Base|ActiveRecord
- * @throws InvalidConfigException
- */
- protected static function getModel()
- {
- if (isset(static::$baseFile) && !empty(static::$baseFile)) {
- if (is_string(static::$baseFile)) {
- return Yii::createObject(['class' => 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);
- }
- }
|