| | <?php |
| |
|
| | namespace app\common\model; |
| |
|
| | use think\Config as ThinkConfig; |
| | use think\Model; |
| | use think\Db; |
| | use think\Cache; |
| |
|
| | class Base extends Model |
| | { |
| | protected $tablePrefix; |
| | protected $primaryId; |
| | protected $readFromMaster; |
| |
|
| | |
| | protected function initialize() |
| | { |
| | |
| | parent::initialize(); |
| | |
| | $this->tablePrefix = isset($this->tablePrefix) ? $this->tablePrefix : ThinkConfig::get('database.prefix'); |
| | $this->primaryId = isset($this->primaryId) ? $this->primaryId : $this->name . '_id'; |
| | $this->readFromMaster = isset($this->readFromMaster) ? $this->readFromMaster : false; |
| | |
| | if (method_exists($this, 'createTableIfNotExists')) { |
| | $this->createTableIfNotExists(); |
| | } |
| | } |
| |
|
| | public function getCountByCond($cond) |
| | { |
| | $query_object = $this; |
| | if ($this->readFromMaster === true) { |
| | $query_object = $query_object->master(); |
| | } |
| | return (int)$query_object->where($cond)->count(); |
| | } |
| |
|
| | public function getListByCond($offset, $limit, $cond, $orderby = '', $fields = "*", $transform = false) |
| | { |
| | $orderby = $orderby ?: ($this->primaryId . " DESC"); |
| | $query_object = $this; |
| | if ($this->readFromMaster === true) { |
| | $query_object = $query_object->master(); |
| | } |
| | $list = $query_object->where($cond)->field($fields)->order($orderby)->limit($offset . ',' . $limit)->select(); |
| | if (!$list) { |
| | return []; |
| | } |
| | $final = []; |
| | foreach ($list as $row) { |
| | $row_array = $row->getData(); |
| | if ($transform !== false) { |
| | $row_array = $this->transformRow($row_array, $transform); |
| | } |
| | $final[] = $row_array; |
| | } |
| | return $final; |
| | } |
| |
|
| | public function transformRow($row, $extends = []) { |
| | return $row; |
| | } |
| | } |